Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple Group By

Author  Topic 

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 15:28:13
This should be easy. In fact, it's so easy I can't understand why it isn't running!

We are looking for a simple summary of each driver and how many orders they've done on a given range of dates. Here's the code:


DECLARE @Calendar1 AS DateTime
DECLARE @Calendar2 AS DateTime
SET @Calendar1 = '{{{ Please choose a start date. }}}'
SET @Calendar2 = '{{{ Please choose an end date. <(non inclusive)> }}}'

SELECT OD.Driver, O.ReadyTimeFrom, Count(OD.OrderID)

FROM tblOrder AS O
LEFT JOIN tblOrderDrivers AS OD ON OD.OrderID = O.OrderID

WHERE O.DueTimeTo BETWEEN @Calendar1 AND @Calendar2

GROUP BY OD.Driver, O.ReadyTimeFrom


So, what have I done wrong?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-28 : 15:42:50
What is it doing? Is it giving you too many rows, or does it have some other problem? My guess is that you should not group by ReadyTimeFrom (but it is a wild guess because I don't know the nature of the data)
DECLARE @Calendar1 AS DateTime
DECLARE @Calendar2 AS DateTime
SET @Calendar1 = '{{{ Please choose a start date. }}}'
SET @Calendar2 = '{{{ Please choose an end date. <(non inclusive)> }}}'

SELECT OD.Driver, -- O.ReadyTimeFrom,
Count(OD.OrderID)

FROM tblOrder AS O
LEFT JOIN tblOrderDrivers AS OD ON OD.OrderID = O.OrderID

WHERE O.DueTimeTo BETWEEN @Calendar1 AND @Calendar2

GROUP BY OD.Driver --, O.ReadyTimeFrom

Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 15:47:24
It is showing a row for each order.

Is there a way for me to attach a screenshot?
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 15:52:56
What I'm trying to get is something like this:

Driver ID, Date, TotalOrders
300, 08-27-2013, 5
300, 08-28-2013, 4
301, 08-27-2013, 7
304, 08-28-2013, 1
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-28 : 16:10:22
You have to upload the image to any of the third part hosting sites and then insert the link into your posting.

Can you give this a try
DECLARE @Calendar1 AS DateTime
DECLARE @Calendar2 AS DateTime
SET @Calendar1 = '{{{ Please choose a start date. }}}'
SET @Calendar2 = '{{{ Please choose an end date. <(non inclusive)> }}}'

SELECT OD.Driver, DATEADD(dd,DATEDIFF(dd,0,O.ReadyTimeFrom),0) AS [Date],
Count(OD.OrderID)

FROM tblOrder AS O
LEFT JOIN tblOrderDrivers AS OD ON OD.OrderID = O.OrderID

WHERE O.DueTimeTo BETWEEN @Calendar1 AND @Calendar2

GROUP BY OD.Driver , DATEADD(dd,DATEDIFF(dd,0,O.ReadyTimeFrom),0)
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 16:27:27
That worked perfectly. Thank you very much James.

If I want to show the day of the week, would I just add datename(dw,getdate(O.ReadyTimeFrom)) ?
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-28 : 16:48:15
quote:
Originally posted by dwdwone

That worked perfectly. Thank you very much James.

If I want to show the day of the week, would I just add datename(dw,getdate(O.ReadyTimeFrom)) ?

You don't need the getdate in there. So it would be:
DECLARE @Calendar1 AS DateTime
DECLARE @Calendar2 AS DateTime
SET @Calendar1 = '{{{ Please choose a start date. }}}'
SET @Calendar2 = '{{{ Please choose an end date. <(non inclusive)> }}}'

SELECT OD.Driver, DATEADD(dd,DATEDIFF(dd,0,O.ReadyTimeFrom),0) AS [Date],
DATENAME(dw,O.ReadyTimeFrom) AS DayOfTheWeek,
Count(OD.OrderID)

FROM tblOrder AS O
LEFT JOIN tblOrderDrivers AS OD ON OD.OrderID = O.OrderID

WHERE O.DueTimeTo BETWEEN @Calendar1 AND @Calendar2

GROUP BY OD.Driver , DATEADD(dd,DATEDIFF(dd,0,O.ReadyTimeFrom),0),
DATENAME(dw,O.ReadyTimeFrom)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-08-28 : 16:57:09
WHERE O.DueTimeTo >= @Calendar1 AND O.DueTimeTo < @Calendar2



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 17:01:08
Why use a range rather than BETWEEN, Oh Patron Saint?
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 17:04:11
Thank you James. Because of your assistance, my head has fewer bruises and the wall fewer dents.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-28 : 17:15:45
You are very welcome.

The reason Swepeso suggested the range rather than between is to take care of an edge case correctly.

-- WHERE O.DueTimeTo BETWEEN @Calendar1 AND @Calendar2 translates to this:
WHERE O.DueTimeTo >= @Calendar1 AND O.DueTimeTo <= @Calendar2

-- What you probably want if your query has to be non-inclusive is the strictly less than rather than less than or equal to.
WHERE O.DueTimeTo >= @Calendar1 AND O.DueTimeTo < @Calendar2
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 17:29:23
Awesome. Thank you very much.
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-28 : 19:14:14
I am using DATENAME(dw,O.ReadyTimeFrom), but the previous day is displaying. For example, I am seeing two groups of orders (rows) for 8-26-13, one that says Monday, and the other says Tuesday. When I drill down to the orders, one was on Monday 8-26-13, the second was on Tuesday 08-27-13. I've verified that I'm using the correct column (O.ReadyTimeFrom), which is a datetime type.

Any ideas where I could be going wrong? This should be a really simple query.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-29 : 09:34:00
quote:
Originally posted by dwdwone

I am using DATENAME(dw,O.ReadyTimeFrom), but the previous day is displaying. For example, I am seeing two groups of orders (rows) for 8-26-13, one that says Monday, and the other says Tuesday. When I drill down to the orders, one was on Monday 8-26-13, the second was on Tuesday 08-27-13. I've verified that I'm using the correct column (O.ReadyTimeFrom), which is a datetime type.

Any ideas where I could be going wrong? This should be a really simple query.

I am not quite following what you are saying here. If the ReadyTimeFrom is indeed showing two dates, wouldn't/shouldn't it display as such in the results? If it should not, I would examine the logic of whether that column is being populated correctly, or whether that is the column you should be using.
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-29 : 12:04:42
Well, there is only one value in the ready time column, which is a datetime column. Yet in some rows, it will display incorrectly. For example, an instance when the column has a value of August 28, 2013 08:30 a.m. it may show Thursday when it should be showing Wednesday. It's very odd. The only thing I can think of is that the function I'm using to separate out the date is not what I should be using.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-08-29 : 12:26:25
Is ReadyTimeFrom DATETIMEOFFSET datatype?



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-29 : 13:06:06
I'm about 90% certain that it is.
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-08-29 : 15:50:33
Here's a good way to explain it. I added in the full column O.ReadyTimeFrom, right after convert(varchar,( dateadd(hour,-1,O.ReadyTimeFrom) ),101) AS Date

Here is a sampling:

Fleet 1, 08-28-2013, 08-28-2013 11:00:99 PM. 1, 21
Fleet 1, 08-28-2013, 08-29-2013 12:55:00 AM, 1, 15
Go to Top of Page
   

- Advertisement -