Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple SQL queries in 1

Tags:

sql

sql-server

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?

like image 343
Alex D Avatar asked Dec 05 '25 12:12

Alex D


1 Answers

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)
like image 165
Mahmoud Gamal Avatar answered Dec 08 '25 05:12

Mahmoud Gamal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!