SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple Group By
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dwdwone
Yak Posting Veteran

USA
68 Posts

Posted - 08/28/2013 :  15:28:13  Show Profile  Reply with Quote
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?

Edited by - dwdwone on 08/28/2013 15:37:49

James K
Flowing Fount of Yak Knowledge

3639 Posts

Posted - 08/28/2013 :  15:42:50  Show Profile  Reply with Quote
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

USA
68 Posts

Posted - 08/28/2013 :  15:47:24  Show Profile  Reply with Quote
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

USA
68 Posts

Posted - 08/28/2013 :  15:52:56  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3639 Posts

Posted - 08/28/2013 :  16:10:22  Show Profile  Reply with Quote
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

USA
68 Posts

Posted - 08/28/2013 :  16:27:27  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3639 Posts

Posted - 08/28/2013 :  16:48:15  Show Profile  Reply with Quote
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

Sweden
30241 Posts

Posted - 08/28/2013 :  16:57:09  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

USA
68 Posts

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

dwdwone
Yak Posting Veteran

USA
68 Posts

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

James K
Flowing Fount of Yak Knowledge

3639 Posts

Posted - 08/28/2013 :  17:15:45  Show Profile  Reply with Quote
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

USA
68 Posts

Posted - 08/28/2013 :  17:29:23  Show Profile  Reply with Quote
Awesome. Thank you very much.
Go to Top of Page

dwdwone
Yak Posting Veteran

USA
68 Posts

Posted - 08/28/2013 :  19:14:14  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3639 Posts

Posted - 08/29/2013 :  09:34:00  Show Profile  Reply with Quote
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

USA
68 Posts

Posted - 08/29/2013 :  12:04:42  Show Profile  Reply with Quote
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

Sweden
30241 Posts

Posted - 08/29/2013 :  12:26:25  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Is ReadyTimeFrom DATETIMEOFFSET datatype?



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

dwdwone
Yak Posting Veteran

USA
68 Posts

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

dwdwone
Yak Posting Veteran

USA
68 Posts

Posted - 08/29/2013 :  15:50:33  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000