3

I kinda got stuck at trying to combine a few queries. What I got is the table holding sort of statistics and reference to another table.

to get statistics report I'm running (short version):

SELECT COUNT(id) 
from [Actions] 
where date between '2012-01-01 00:00:00' AND '2012-01-01 23:59:59' 
  AND [Action]='request'

The question is if I want to get every-day statistics during the specified time period, how should this query look like? I understand if I change start and end dates, I'll get statistics for the whole specified period and not grouped by day. What am I missing?

1 Answer 1

3

Just GROUP BY date after eleminating the time part like so:

SELECT 
  CONVERT(VARCHAR(10), [date], 121) ByDay, COUNT(id) 
FROM [Actions] 
WHERE date BETWEEN '2012-01-01 00:00:00' AND '2012-01-01 23:59:59' 
  AND [Action]='request'
GROUP BY CONVERT(VARCHAR(10), [date], 121)
Sign up to request clarification or add additional context in comments.

3 Comments

I knew I was missing something! Thanks, but I'm guessing the right form will be to use 101 and not 121 to prevent the query to group records by minutes and seconds. am I right?
@AlexD - I think the two will work for you. 121 will work since it will get only the first 10 character by varchar(10) which are the date part only.
Anyways, thanks so much for reminding me how does GROUP BY works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.