Link to home
Create AccountLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

sql query help

how would I get most popular sales time. Orderdate is dateTime field

Select orderdate
FROM Orders
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

SELECT TOP(1) Orderdate FROM Orders
I think that this depends on the precision of your Order time and what sort of time interval you want to group by. If you're using a datetime datatype and the system records to milliseconds, the data is possibly not very helpful, But if you grouped into a larger interval and counted the number, you'd get a more meaningful result.


Kelvin
Avatar of erikTsomik

ASKER

I would like to group by from 1am - 5am, 5:00 -- 7am, 7am -- 9am,9am -- 12pm, 12pm-- 3pm,3pm - 6pm,6pm--9pm,9pm--12am
ASKER CERTIFIED SOLUTION
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I get the following data
Hour    Orders
0	286
1	88
2	48
3	24
4	24
5	74
6	278
7	722
8	1356
9	3013
10	3636
11	3901
12	3801
13	3703
14	3724
15	4059
16	3722
17	3201
18	2433
19	2615
20	3192
21	3237
22	2185
23	937

Open in new window

That's a count orders by hour. i.e. 0 is from midnight to 1 am.
You now need to consider how you want that further broken down (by Year, Month etc...). The n we can look at the grouping you suggested.