Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I need a help in MS SQL Express query. I have a table named "tblTotalCust" and the fields are:
[Date] date, [ServedTime] date, [TotalCustomer] int.Example data:
[Date] [ServedTime] [TotalCustomer]
8/9/2008 12:00:00AM 8/9/2008 9:23:00AM 12
8/9/2008 12:00:00AM 8/9/2008 9:27:00 AM 7
8/9/2008 12:00:00AM 8/9/2008 9:37:00AM 9
8/9/2008 12:00:00AM 8/9/2008 10:45:00AM 25
8/9/2008 12:00:00AM 8/9/2008 10:57:00AM 11
8/9/2008 12:00:00AM 8/9/2008 11:32:00AM 6In MS Access, my query is :
SELECT Date, TimeSerial(0, ((HOUR(ServedTime) * 60 + MINUTE(ServedTime))\30)*30,0) AS TimeOfDay, SUM(TotalCustomer) AS TotalCustomer GROUP BY Date, TimeSerial(0,
((HOUR(ServedTime) * 60 + MINUTE(ServedTime))\30)*30,0)
This will produce a sum TotalCustomer in each 30 minute of ServedTime as below:
[Date] [TimeOfDay] [TotalCustomer]
8/9/2008 12:00:00AM 8/9/2008 9:00:00AM 19
8/9/2008 12:00:00AM 8/9/2008 9:30:00AM 9
8/9/2008 12:00:00AM 8/9/2008 10:30:00AM 36
8/9/2008 12:00:00AM 8/9/2008 11:30:00AM 6But how do i write this query in MS SQL Express? Please help. Thanks

(edit: i'd originally written the query for a whole different table with different fields. had to change a field name to match.)
I don't remember much about Access, especially what TimeSerial does and whether/how it's used in MSSQL, so i did a MSSQL query from scratch. Basically, it subtracts (minutes % 30) minutes and (seconds) seconds from TimeServed to get the half hour intervals.
SELECT [Date],
DATEADD(SECOND, -((DATEPART(MINUTE, TimeServed) % 30) * 60 + DATEPART(SECOND, TimeServed)), TimeServed) TimeOfDay,
SUM(TotalCustomer) TotalCustomer
FROM tblTotalCust
GROUP BY [Date], DATEADD(SECOND, -((DATEPART(MINUTE, TimeServed) % 30) * 60 + DATEPART(SECOND, TimeServed)), TimeServed)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |