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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Orders From the Past 24 Hours

Author  Topic 

josethegeek
Starting Member

45 Posts

Posted - 2004-08-18 : 14:20:54
Hello,
Maybe someone can help me out...

I need to return the total orders from the past 24 hours, grouped by the hour. So if it's 11 am, I would want to return the orders from the previous date starting at 11 am till today's date ending at 11 am.

I basically have an orders table that records the datetime the order was placed on.

tbl_Orders
----------
Order_ID
Order_Total
Order_Date

Would somebody be kind enough to help me out?

Thanks,
Jose

josethegeek
Starting Member

45 Posts

Posted - 2004-08-18 : 14:45:32
Thanks,
But I figured it out myself. Ill just do a dateadd and subtract 24 hours from now. Thanks
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-08-18 : 15:01:01
Probably something like:

SELECT [Hour] = DATEADD(Hour, 0, DATEDIFF(Hour, 0, Order_Date)),
[Value] = SUM(Order_Total)
FROM dbo.tbl_Orders
WHERE Order_Date >= DATEADD(Hour, 0, DATEDIFF(Hour, 0, GetDate()) - 24)
GROUP BY DATEADD(Hour, 0, DATEDIFF(Hour, 0, Order_Date))

Kristen
Go to Top of Page

josethegeek
Starting Member

45 Posts

Posted - 2004-08-18 : 16:46:22
quote:
Originally posted by Kristen

Probably something like:

SELECT [Hour] = DATEADD(Hour, 0, DATEDIFF(Hour, 0, Order_Date)),
[Value] = SUM(Order_Total)
FROM dbo.tbl_Orders
WHERE Order_Date >= DATEADD(Hour, 0, DATEDIFF(Hour, 0, GetDate()) - 24)
GROUP BY DATEADD(Hour, 0, DATEDIFF(Hour, 0, Order_Date))

Kristen



Kristen,
Thanks, that is exactly what I had in mind.

Jose
Go to Top of Page
   

- Advertisement -