MySQL group by month and day for current year - mysql

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 !!!

Related

Finding Fifth Saturdays Mathematically?

So, where I work, my employees work alternating Saturdays: some employees are assigned to work the 1st and 3rd Saturdays of each month, some are assigned to work the 2nd and 4th Saturdays of each month.
One small problem arises: there are four months in the year which have five Saturdays. Which is easy enough to work around: 1st/3rd Employees work the 1st and 3rd Fifth Saturdays in the year and so on.
Some years have five months with fifth Saturdays, but we're not talking about that right now.
Anyway, to generate schedules for my employees, I first use the following code to generate a list of dates:
select
curdate() - interval (a.a + (10 * b.a) + (100 * c.a)) day as Date
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
) as a
Then, I select the dates from the subquery, and join them to my Scheduling table to create a list of what each employee works on each date.
All of this works fine until Fifth Saturdays rear their ugly heads. At the moment, I use the following code to mathematically figure out which fifth saturday of the year it is:
mod(((datediff(date_add(a.date, interval (7-dayofweek(a.date)) day),date_add(subdate(a.Date,dayofyear(a.date-1)), interval (1-dayofweek(subdate(a.Date,dayofyear(a.date-1)))) day))+1)/7),4)
Or, to summarize, I find the number of days that have elapsed from the current first Sunday of the year to the current Saturday of this week, divide it by seven to figure out how many Saturdays have passed since then, and then find the modulo of 4 to figure out how many "extra" fifth Saturdays have passed since then.
...and this code will work perfectly fine unless the Fifth Saturday happens to fall on a week which is a multiple of four. Thankfully, that doesn't happen at all this year, but next year I'll need to figure out how to deal with this.
Is there a better way to mathematically figure out which fifth Saturday of the month a given fifth Saturday is?
Oh man. I was way overthinking the SQL piece of this. It was actually pretty easy. Create three temporary tables: one to hold the completed date table, one to hold all of the dates of interest in your period, and a third table specifically to find Fifth Saturdays:
drop temporary table if exists DATES;
create temporary table DATES (AdherenceDates date, WhichSaturday int,
WhichFifthSaturday int);
create temporary table DATES1 (AdherenceDates date, WhichSaturday int);
create temporary table DATES2 (FifthSaturdayDates date, WhichFifthSaturday
int auto_increment, primary key (WhichFifthSaturday));
insert into DATES1 (AdherenceDates, WhichSaturday)
select
a.Date as AdherenceDates,
case
when day(adddate(a.date,(7-dayofweek(a.date)))) <= 7
then 1
when day(adddate(a.date,(7-dayofweek(a.date)))) <= 14
then 2
when day(adddate(a.date,(7-dayofweek(a.date)))) <= 21
then 3
when day(adddate(a.date,(7-dayofweek(a.date)))) <= 28
then 4
when day(adddate(a.date,(7-dayofweek(a.date)))) > 28
then 5
end as WhichSaturday
from
(select
curdate() - interval (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a)) day
as Date
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
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 d
) as a
where dayofweek(a.Date) <> 1
and a.Date >= '2017-01-01'
order by a.Date asc
;
insert into DATES2 (FifthSaturdayDates)
select
a.Date as AdherenceDates
from
(select
curdate() - interval (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a)) day
as Date
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
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 d
) as a
where dayofweek(a.Date) = 7
and a.Date >= '2017-01-01'
and day(a.Date) > 28
order by a.date asc;
insert into DATES (AdherenceDates, WhichSaturday, WhichFifthSaturday)
select
DATES1.AdherenceDates,
DATES1.WhichSaturday,
DATES2.WhichFifthSaturday
from
DATES1
left join
DATES2
on DATES1.AdherenceDates = DATES2.FifthSaturdayDates;
drop temporary tables DATES1, DATES2;
select * from DATES
order by AdherenceDates asc;

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.

Finding hourly passenger loads given boarding and deboarding time

I have the following table of transport records for a bus company:
CREATE TABLE ride_txn(
passenger_no int(11) pk,
txn_time timestamp,
action varchar(10)
)
where the action could be "Board" or "Deboard".
Say I have 2 rows where for passenger_no. 100, he boarded at 1.30pm and alighted at 4.30pm.
passenger_no txn_time action
100 13:30:00 Board
100 16:30:00 Deboard
Can I write an sql query to retrieve the hours that he is in the bus? I do the count at the beginning of each hour so he was in the bus at 2pm, 3pm and 4pm. In other words, I am trying to get something like
passenger_no hour_in_bus
100 2
100 3
100 4
Here's a start:
select
passenger_no, hr
from
ride_txn rt,
(
select 0 hr 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
) hrs
where
action = 'Board' and
hrs.hr between
hour(txn_time) /* could add 3599 seconds to only count top of the hour */
and
(
select min(txt_time) from ride_txn rt2
where
rt2.passenger_no = rt.passenger_no
and rt2.txt_time > rt.txt_time and action = 'Deboard'
)
I had to assume that the events will pair off correctly and also that the "deboard" time will be greater than the "board" time. So nothing spans midnight and it takes place within a single day. It would handle multiple pairs within the day though.
I'm not sure if hour() is actually a MySQL function but I'm sure you can find the equivalent one. I'm also assuming it returns a number from 0 to 23.
select id,y.tm from
(
select id,
max(case when action = 'Deboard' then hour(dt) end) as d_time,
max(case when action = 'Board' then hour(dt) end) as b_time
from tablename
group by id) x
join
(
select distinct hour(dt) as tm from tablename
) y
on y.tm between x.b_time and x.d_time
This would work assuming you have all the 24 hours in the table.
with cte as (select a.passenger_no,case when minute(a.txn_time) > 0 or Second(a.txn_time) > 0 then hour(a.txn_time) + 1 else hour(a.txn_time) end as brd_time,
hour(b.txn_time) debrd_time,
from ride_txn a inner join ride_txn b
on a.passenger_no = b.passenger_no where a.action = 'Board' and b.action = 'DeBoard')
select
passenger_no, hr_12
from
cte,
(
select 0 hr, 0 hr_12 union all select 1,1 union all select 2,2 union all
select 3,3 union all select 4,4 union all select 5,5 union all
select 6,6 union all select 7,7 union all select 8,8 union all
select 9,9 union all select 10,10 union all select 11,11 union all
select 12,12 union all select 13,1 union all select 14,2 union all
select 15,3 union all select 16,4 union all select 17,5 union all
select 18,6 union all select 19,7 union all select 20,8 union all
select 21,9 union all select 22,10 union all select 23,11
) hrs
where
hrs.hr between brd_time and debrd_time

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.