select 1 when there is a missing date - MYSQL - mysql

I want to have a condition or return a result(1) when my query detects that I have a missing date and I'm calling it between two dates from the user.
As you can see I'm missing 3/22/2016 until 3/26/2016. I want to get a result from my query that there are missing dates in my query.
Can you give me hints on how to do this?
Here's my query:
select DISTINCT DOB from DATES where STR_TO_DATE(DOB, '%m/%d/%Y') BETWEEN '20160301' AND '20160331'

This will give only date is missing or not.
select case when count(DISTINCT DOB) = datediff('20160331','20160301') + 1 then 1 else 0 end
from DATES
where STR_TO_DATE(DOB, '%m/%d/%Y') BETWEEN '20160301' AND '20160331'
Below query will give you total dates present in it.
select asodate ,t1.dob
from
( SELECT #row := #row + interval 1 day as asofdate
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 #row:='2016-03-01' - interval 1 day) a
where #row < '2016-03-31') generateCalender
left join
( select DISTINCT STR_TO_DATE(DOB, '%m/%d/%Y') as dob
from DATES
where STR_TO_DATE(DOB, '%m/%d/%Y') BETWEEN '20160301' AND '20160331' ) t1
on asofdate = t1.dob
You can easily find out all missing date by adding where condition.
where t1.dob is NULL

Related

mysql: Row count by date in between

From the screenshot shown, it is failed/missing to show the count of date
'2018-06-23' ,2018-06-25,2018-06-26,2018-06-27 , can you help me to write the correct query so that i get count of missing dates. I am trying to get count of total number of rows on particular date(created_date).
SELECT count(book_id) as count,created_date as cdate
FROM `bookdetails`
WHERE (created_date >= '2018-06-21' AND created_date <='2018-06-27')
GROUP by DATE(created_date)
ORDER BY MIN(created_date)
Expected output:
count | cdate
----------------------
98 2018-06-21
39 2018-06-22
0 2018-06-23 //because no data
39 2018-06-24
XX 2018-06-25
XX 2018-06-26
XX 2018-06-27
Table: bookdetails structure
Date generation and join need to show date that doest exist
select count(book_id) as count,date(C_date) as cdate from
(
select * from
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) C_date from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where C_date between '2018-01-01' and '2018-12-31'
) All_date
left join bookdetails
on date(created_date)=All_date.C_date
where ( date(All_date.C_date) >= '2018-06-21' AND date(All_date.C_date) <='2018-06-27')
group by date(C_date)
order by date(C_date) asc
Why not simply use BETWEEN instead of => and <=.
Usage:
SELECT * FROM bookdetails
WHERE created_date BETWEEN lowerdate AND upperdate
Show us your backend. We'll be able to assist you better.
And what is the expected result?

mysql Query to get list of months name from two dates

I would like to create a list of months name between two dates. What is the best way to do via mysql query.
Select start_date, end_date from table where id=123
above query result - start date: '2016-01-15' end date: '2017-04-28'
The final result should be:
Jan-16
Feb-16
Mar-16
......
......
......
Feb-17
Mar-17
Apr-17
Thanks for the help!
dont have a time need to go now just Search for Date_Format to this "date(thisday)"
select CONCAT_WS('-',monthname(date(thisday)),substr(thisday from 9 FOR 2)) from
(SELECT ADDDATE('2017-01-01', INTERVAL #i:=#i+1 DAY) AS thisday
FROM (
SELECT a.a
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
JOIN (SELECT #i := -1) r1
WHERE
#i < DATEDIFF('2017-01-31', '2017-01-01')) as a
Some result:
January-18
January-19
January-20
January-21
January-22
January-23
January-24
January-25
January-26
January-27
January-28
January-29

how to calculate total number of saturday and sunday between two dates in mysql

SELECT
floor(
datediff('2016-08-01','2016-07-01') / 7) * 2 + (
CASE WHEN
IF (weekday('2016-08-01') >= 5, 4,weekday('2016-08-01')) >=
IF (weekday('2016-07-02') >= 5, 4,weekday('2016-07-01'))
THEN
IF (weekday('2016-08-01') >= 5, 4,weekday('2016-08-01')) -
IF (weekday('2016-07-01') >= 5, 4,weekday('2016-07-01'))
ELSE
5 +
IF (weekday('2016-08-01') >= 5, 4,weekday('2016-08-01')) -
IF (weekday('2016-07-01') >= 5, 4, weekday('2016-07-01'))
END
) weekdays;
when i execute this code it always return 9 i don't know where i am doing mistake please suggest me where am doing wrong. while between Saturday and Sunday between given date should be 10 please suggest me
If you don't have a table having all the dates between your start date and end date (inclusive) then you need to adopt a query which will create all the dates between your given date range (inclusive) first. Then use WEEKDAY function of MySQL to check whether the day is Saturday or Sunday
SELECT
dateTable.Day,
DAYNAME(dateTable.Day) AS dayName
FROM
( SELECT ADDDATE('2016-07-01', INTERVAL #i:=#i+1 DAY) AS DAY
FROM (
SELECT a.a
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
JOIN (SELECT #i := -1) r1
WHERE
#i < DATEDIFF('2016-08-01', '2016-07-01')
) AS dateTable
WHERE WEEKDAY(dateTable.Day) IN (5,6)
ORDER BY dateTable.Day;
WORKING DEMO
Note:
WEEKDAY returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
EDIT:
If you need only count:
SELECT
COUNT(*) AS total
FROM
( SELECT ADDDATE('2016-07-01', INTERVAL #i:=#i+1 DAY) AS DAY
FROM (
SELECT a.a
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
JOIN (SELECT #i := -1) r1
WHERE
#i < DATEDIFF('2016-08-01', '2016-07-01')
) AS dateTable
WHERE WEEKDAY(dateTable.Day) IN (5,6)
Demo

How to display all date values as null in a date range of mysql?

I have a table of booking and need to find the count of booking in a specific date range in MYSQL. But it is giving me only non empty values. I need to show the non existence date within range as NULL in my select query. Here is my query:
SELECT DATE_FORMAT(booking_created_on, '%b %e') AS dateValue , COUNT(booking_id) AS cnt FROM booking WHERE booking_created_on BETWEEN '2016/02/01' AND '2016/02/08' GROUP BY booking_created_on
My result from this query is :
And my expected result is:
Please help me in mysql query for getting the desired result.
Try This
SELECT DATE_FORMAT(selected_date, '%b %e'), cnt
FROM(
SELECT
str_to_date(booking_created_on, '%Y/%m/%d') AS selected_date ,
COUNT(booking_id) AS cnt
FROM booking
WHERE booking_created_on BETWEEN '2016/02/01' AND '2016/02/08'
GROUP BY booking_created_on
UNION
SELECT selected_date,0 AS cnt FROM
(SELECT adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) v
where selected_date BETWEEN '2016-02-01' and '2016-02-08' AND selected_date NOT IN (
SELECT
DISTINCT(str_to_date(booking_created_on, '%Y/%m/%d')) FROM booking
WHERE booking_created_on BETWEEN '2016/02/01' AND '2016/02/08'
)
) AS T1
ORDER BY selected_date ;
In my query, first i get all date and count in your table and in second subquery i get all missing date which is not in your table between two dates. show result order by date. this is lengthy query so may be it will be some error so if any error, comment

Select all dates between two dates not using a table (generate list of dates)

Is there a way to get all dates between two dates without using any MySQL table
Something like:
SELECT date BETWEEN '2012-02-10' AND '2012-02-15'
that would result in this:
out put date list
2012-02-10
...
2012-02-15
Here is the query:
This query gives proper result in both the databases : MariaDB & MySQL.
SELECT ADDDATE('2012-02-10', INTERVAL #i:=#i+1 DAY) AS DAY
FROM (
SELECT a.a
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
JOIN (SELECT #i := -1) r1
WHERE
#i < DATEDIFF('2012-02-15', '2012-02-10')