Take count based on rage of date SQL - mysql

I have below mentioned sql query which gives me simple count as on that date or if i use where and and condition for date it gives me consolidated count between the date.
select count(*) from A where date(date_1) = '2017-06-01';
I want count for a range of date for separate count for each date.
select count(*) from A where date(date_1) >= '2017-06-01' and date(date_1)<='2017-06-30';
Desired output:
Date Count
2017-06-01 25
2017-06-02 20
2017-06-03 15
- -
- -
2017-06-30 10

Something like the following might work:
SELECT DATE(date_1), COUNT(*)
FROM A
GROUP BY DATE(date_1)
COUNT, being an aggregate function, is often combined with the GROUP BY clause in order to get aggregate results per group (in this case, counts per date).
WHERE clauses can of course be added as desired, right before the GROUP BY clause.

All you need is to use group by clause like:
SELECT date(date_1),Count(*) as [Count]
FROM A
WHERE date(date_1) >= '2017-06-01' and date(date_1)<='2017-06-30'
GROUP BY date(date_1)

The following solution also reports days with no noted incidents. The example is devised for the month of February:
SELECT (DATE'2018-01-31' + INTERVAL x.n DAY) dd
, COALESCE(sqt.c, 0) incidents
FROM (select 1 n union all select 2 n union all select 3 n union all select 4 n union all select 5 n union all select 6 n union all select 7 n union all select 8 n union all select 9 n union all select 10 n union all select 11 n union all select 12 n union all select 13 n union all select 14 n union all select 15 n union all select 16 n union all select 17 n union all select 18 n union all select 19 n union all select 20 n union all select 21 n union all select 22 n union all select 23 n union all select 24 n union all select 25 n union all select 26 n union all select 27 n union all select 28 n) x
LEFT JOIN (
SELECT date1 d
, count(*) c
FROM t
GROUP BY date1
) sqt
ON sqt.d = (DATE'2018-01-31' + INTERVAL x.n DAY)
;
The standalone demo can be viewed here.
The sequence generator has been shamelessly adopted from this SO answer.

Related

MySQL query - convert to left join to show results with zero?

I'm not very good when it comes to using joins - so I have a single table where I'm counting the number of records that meet certain conditions, and returns those counts by week. The problem is, I need the weeks that have a zero count too....I tried to get this to work with a left join, but I'm struggling...any help appreciated: (Stamp is a datetime field)
Query:
SELECT week(stamp), count(*) AS mycount, YEAR(stamp) as theyear
FROM merges
WHERE completed = 1
AND stamp BETWEEN '2017/4/1 00:00:00' AND '2017/6/1 00:00:00' GROUP BY week(stamp)
This returns:
week(stamp) | mycount | theyear
15 | 21 |2017
17 | 10 |2017
18 | 62 |2017
19 | 13 |2017
20 | 76 |2017
21 | 22 |2017
Notice week 16 is missing? I need to have this result included in the above, like:
16 | 0 |2017
I appreciate any help - I know this isn't too difficult, but I'm pulling my hair out trying to understand how to do this while I read other posts....
select weekValue, yearValue, coalesce(mycount,0)
from
( SELECT distinct week(#startDate := #startDate + Interval 1 day) as weekValue,
year(#startDate := #startDate + Interval 1 day) as yearValue
FROM
(select 0 union all select 1 union all select 3 union all select 4
union all select 5 union all select 6 union all select 6 union all select 7
union all select 8 union all select 9) t,
(select 0 union all select 1 union all select 3
union all select 4 union all select 5 union all select 6
union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4,
(SELECT #startDate := '2017-03-31 00:00:00' ) as g
where
#startDate < '2017-06-01 00:00:00' ) as generateWeekYear left join
(SELECT week(stamp) as theweek, count(*) AS mycount, YEAR(stamp) as theyear
FROM merges
WHERE completed = 1
AND stamp BETWEEN '2017/4/1 00:00:00' AND '2017/6/1 00:00:00' GROUP BY week(stamp) ) as actualQuery
on generateWeekYear.weekValue = actualQuery.theweek
and generateWeekYear.yearValue = actualQuery.theyear
Let me explain the above query,
Sub Query generateWeekYear = This is used to genearate distinct week and year based on two inputs
lets say startDate and endDate. startDate should be 1 day less to actual startDate. Because if you do not
subtract 1 day then there might chance to loose one week.
Now you have all week and year which needs to be displayed.
Now you are thinking generateWeekYear is going to be more time to execute but this is not case. You can
check this generate an integer sequence in MySQL.
After that you simply join your table with above table and you can get your required result.

mysql select date day by day

I have table shown below :
login
date user
2016-11-23 1
2016-11-23 2
2016-11-23 3
2016-11-25 2
2016-11-25 5
2016-11-27 1
from above table what I want to get is like this:
date count(*)
2016-11-21 0
2016-11-22 0
2016-11-23 3
2016-11-24 0
2016-11-25 2
2016-11-26 0
2016-11-27 1
But, because there are only dates 2016-11-23 and 2016-11-25 and 2016-11-27, when I query like this :
select date, count(*)
from login
where date between (current_date()-interval 7 day) and current_date()
group by date
order by date asc
It can't get result like what I really want to get. Is that result possible from my login table?
One way is to generate all days before JOIN
select GenDate, count(Date)
from login
right join
(select a.GenDate
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as GenDate
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.GenDate between (current_date()-interval 7 day) and current_date())x
ON x.GenDate=login.Date
group by GenDate
order by GenDate asc
Use a derived table with the wanted dates :
SELECT t.date, count(s.date)
FROM (SELECT '2016-11-21' as `date` UNION ALL
SELECT '2016-11-22' as `date` UNION ALL
...) t
LEFT JOIN login s
ON(t.date = s.date)
WHERE
t.date between (current_date()-interval 7 day) and current_date()
GROUP BY t.date
ORDER BY t.date
This is a very well known problem in programming. There are several solutions.
Go over the result with PHP, and fill the missing days in the resulting array.
AS sagi proposed, create a separate table that contains all the dates in the range of days your application works with, then you can JOIN that table with your query. One of the issues is that from time to time you have to add more days to this table, if you suddenly have missing days in future or in past.

Group by month or 0 if not results?

I have the following query
SELECT MONTH(date_added), COUNT(*)
FROM invite
WHERE YEAR(date_added) = 2013
GROUP BY MONTH(date_added)
And it works perfectly fine, but my problem is if there are no results for a month it doesn't output the month, I need it to say 0 instead.
I don't want to create a table with all 12 month values. And I don't want to run 12 queries, is there another way to do this?
You don't have to "create a table with 12 month values". You can just do it in the query:
SELECT m.mon, COUNT(i.date_added)
FROM (select 1 as mon union all select 2 union all select 3 union all select 4 union all
select 5 union all select 6 union all select 7 union all select 8 union all
select 9 union all select 10 union all select 11 union all select 12
) m left outer join
invite i
on m.mon = i.month(date_added) and year(date_added) = 2013
GROUP BY m.mon;
Here's a cheesy way to do it:
create table months (int monthnum);
Insert the numbers 1 through 12 into months, so it's just a table with 1 column and 12 rows.
select monthnum, coalesce(ct, 0) from months left join (
select month(date_added) Mon, count(*) ct from invite
where year(date_added)=2013 group by Mon)
on monthnum = Mon
Coalesce gives you a zero instead of a null if the month is missing.

Mysql result hourly data

I'm trying to count entries grouped per hour.
I've found some useful info inform on different sites and here on: MySQL Group By Hours
But the result is not what I've expected.
With the following code I get:
SELECT CONCAT(Hour, ':00-', Hour+1, ':00') AS Hours,
COUNT(`usage_time`) AS `usage` FROM `usage`
RIGHT JOIN (
SELECT 0 AS Hour
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18
UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21
UNION ALL SELECT 22 UNION ALL SELECT 23
) AS AllHours ON HOUR(`usage_time`) = Hour
WHERE `usage_function` LIKE 'PlayedWholeSong' AND `usage_date` = DATE_SUB(CURDATE(), INTERVAL 0 DAY) OR `usage_time` IS NULL
GROUP BY Hour
ORDER BY Hour
Result:
Hours usage
2:00-3:00 0
4:00-5:00 6
6:00-7:00 2
8:00-9:00 3
9:00-10:00 20
10:00-11:00 1
14:00-15:00 14
15:00-16:00 1
16:00-17:00 32
17:00-18:00 10
As these are entry's from today, I don't have any entries after 19:00.
Also I don't see an entry from 00:00 - 01:00, 03:00 - 04:00 and several others are missing.
But I do want to show a list with every 24 hour and the result, even if there's nothing.
String thing is the result shows a 0 between 02:00 - 03:00.
I've learned a lot today about mysql, but nothing that solves my issue.
I hope you can learn me something, doesn't have to be code, a direction would be great.
I prefer LEFT JOIN over RIGHT JOIN personally. That way you can add your WHERE criteria in your JOIN and it won't constrict your results. Try this:
SELECT CONCAT(Hour, ':00-', Hour+1, ':00') AS Hours,
COUNT(`usage_time`) AS `usage`
FROM
(
SELECT 0 AS Hour
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18
UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21
UNION ALL SELECT 22 UNION ALL SELECT 23
) AS AllHours
LEFT JOIN `usage` ON HOUR(`usage_time`) = Hour
AND `usage_function` LIKE 'PlayedWholeSong'
AND `usage_date` = DATE_SUB(CURDATE(), INTERVAL 0 DAY)
GROUP BY Hour
ORDER BY Hour
Here is a simplified SQL Fiddle.
Good luck.

MySQL group by month and day for current year

I was wondering if this is possible:
I have some data where i have an datetime field. Now i want to make an sql query where i can make groups by month and in each month by day.
Something like this:
Month day COUNT(*)
1 1 200
1 2 300
1 3 500
2 1 600
2 2 0
Why i need this? I need to make an sql query to make an chart XY and show fill this requeriments:
SELECT series,value1,value2 FROM...WHERE...GROUP BY...ORDER BY.
So i want to make each month to be an SERIE, and then each day is value1, and the count value 2
Hope everyone understand my bot question...
Best Regards and tks in advanced
Is this all you're looking for?
SELECT MONTH(m), DAY(d), COUNT(*)
FROM sparkles
WHERE YEAR(y) = 2013
GROUP BY MONTH(m), DAY(d)
If your dates have gaps, you will need to use a date lookup table.
Use the MONTH() and DAYOFMONTH() functions.
Here is the documentation: dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Try this:
SELECT Month, Day, Count(*) FROM yout_table group by Month, Day
First you are going to need a table that holds every day this year:
CREATE TABLE DaysThisYear
(
dt datetime not null,
mm int, dd int,
primary key (dt)
);
INSERT INTO DaysThisYear (dt,mm,dd)
SELECT ymd,MONTH(ymd),DAY(ymd) FROM
(SELECT IFNULL(ymd + INTERVAL 0 SECOND,'1980-01-01 00:00:00') ymd
FROM (SELECT CONCAT(yy,'-',SUBSTR(mm+100,2),'-',SUBSTR(dd+100,2)) ymd,yy
FROM (SELECT 1 dd UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12
UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16
UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION SELECT 20
UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24
UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28
UNION SELECT 29 UNION SELECT 30 UNION SELECT 31 UNION SELECT 32) AAA,
(SELECT YEAR(NOW()) yy,mm FROM
(SELECT 1 mm UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION
SELECT 10 UNION SELECT 11 UNION SELECT 12) M) BBB) AA) A
WHERE YEAR(ymd) <> 1980
ORDER BY ymd;
To see that every day for this year was loaded, run this:
SELECT * FROM DaysThisYear;
Now, if you have a table with a DATETIME column, you can join the DaysThisYear table to it
For example, lets say your table looks like this:
CREATE TABLE mydata
(
id int not null auto_increment,
dt DATETIME,
.
.
.
PRIMARY KEY (id),
KEY dt (dt)
);
You could perform something like this:
SELECT A.mm,A.dd,SUM(IF(ISNULL(B.mm),0,1)) mmdd_count
FROM DaysThisYear A LEFT JOIN
(SELECT MONTH(dt) mm,DAY(dy) dd FROM mydata) B
ON A.mm=B.mm AND A.dd=B.dd;
Give it a Try !!!