How to display the time period of the most widely in use in ms sql server
I have purchase data, such as:
hours the number of purchases
10:00 2
12:00 4
13:00 5
14:00 1
I want to show the period of time with most buyers. So the results would be with the most purchases hour period between 12:00 to 13:00.
Here is my query :
select h.hours, day(d.dates)
from hours h, dates d
group by h.hours, day(d.dates)
having count(h.hours) >= all (select count(h2.hours)
from hours h2, dates d2
group by h2.hours, day(d2.dates))
but the result is: 13:00
Can anyone help me?
hoursanddatestables? Why is 13:00 not the answer you're looking for? – Nick Chammas Jan 17 '12 at 1:44hourstable represents the 1-hour period starting at that time. So,12:00represents the period starting at that time and ending just before it turns 13:00;13:00represents 13:00-14:00; and so on. So if you're looking for the one hour period with the most purchases then your answer is indeed13:00(which represents 13:00-14:00) since 5 purchases were made during that hour. – Nick Chammas Jan 17 '12 at 2:09