like for example.
this month is December 2012 and as we know all the sundays in this month of 2012 are 2,9,16,23,30.
can anyone please tell me how to get all the days of sundays like as shown below in a month using mysql query?
_ _ _ _ _ _
| Sundays |
|- - - - - -|
| 2 |
| 9 |
| 16 |
| 23 |
| 30 |
- - - - - -
Here is a SQLFiddle demo
select DATE_ADD('2012-12-01', INTERVAL ROW DAY) as Date,
row+1 as DayOfMonth from
(
SELECT #row := #row + 1 as row FROM
(select 0 union all select 1 union all select 3
union all select 4 union all select 5 union all select 6) t1,
(select 0 union all select 1 union all select 3
union all select 4 union all select 5 union all select 6) t2,
(SELECT #row:=-1) t3 limit 31
) b
where
DATE_ADD('2012-12-01', INTERVAL ROW DAY)
between '2012-12-01' and '2012-12-31'
and
DAYOFWEEK(DATE_ADD('2012-12-01', INTERVAL ROW DAY))=1
Related
I'm trying to generate a result from a query that list the last 7 days from today (2020/07/15) and the views matching a specific code.
If in that day the code has no views, I want the day to return 0.
Table Format
DAY | CODE | VIEWS
2020-07-10 | 123 | 5
2020-07-11 | 123 | 2
2020-07-12 | 123 | 3
2020-07-15 | 123 | 8
2020-07-15 | 124 | 2
2020-07-15 | 125 | 2
Expected result from code 123
DAY | VIEWS
2020-07-09 | 0
2020-07-10 | 5
2020-07-11 | 2
2020-07-12 | 3
2020-07-13 | 0
2020-07-14 | 0
2020-07-15 | 8
I already found a way to generate the calendar dates from here and adjust to my needs, but I don't know how to join the result with my table.
select * from
(select
adddate(NOW() - INTERVAL 7 DAY, t0) day
from
(select 1 t0
union select 1
union select 2
union select 3
union select 4
union select 5
union select 6
union select 7) t0) v
Any help would by apreceated.
One option uses a recursive query - available in MySQL 8.0:
with recursive cte as (
select current_date - interval 6 day dt
union all
select dt + interval 1 day from cte where dt < current_date
)
select c.dt, coalesce(sum(t.views), 0) views
from cte
left join mytable t on t.day = c.dt
group by c.dt
order by c.dt
You can also manually build a derived table, as you originaly intended to (this would work on all versions of MySQL):
select current_date - interval d.n day dt, coalesce(sum(t.views), 0) views
from (
select 0 n
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
) d
left join mytable t on t.day = current_date - interval d.n day
group by d.n
order by d.n desc
I have sample data like the below .
Start_Dt End_Dt Dur_of_months amount
2020-01-01 2020-04-01 4 800
where I have Start date and End Date along Duration of months.
By dividing amount (800) by 4 = 200.
I want to get running total minus (200) for each month .
output :
mon_dt amount
Jan 2020 800
Feb 2020 600
Mar 2020 400
Apr 2020 200
I have some code to increment the months between start date and End date
SELECT ID, DATE_FORMAT(Startdate + INTERVAL n.n MONTH, '%M %Y') AS Dates
FROM dates
JOIN (
SELECT n10.n * 10 + n1.n * 1 AS n
FROM (
SELECT 0 n 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
) n10
CROSS JOIN (
SELECT 0 n 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
) n1
) n ON Startdate + INTERVAL n.n MONTH <= EndDate
ORDER BY ID, Startdate + INTERVAL n.n MONTH
How to add the Running total in this one . can any one suggest me .
If you are running MySQL 8.0, you can use a recursive query for this:
with recursive cte as (
select
start_dt,
end_dt,
dur_of_months,
1 lvl,
amount initial_amount,
amount
from mytable
union all
select
start_dt + interval 1 month,
end_dt,
dur_of_months,
lvl + 1,
initial_amount,
initial_amount * (1 - lvl / dur_of_months)
from cte
where start_dt < end_dt
)
select date_format(start_dt, '%M %Y') mon_dt, amount from cte order by start_dt
Demo on DB Fiddle:
| mon_dt | amount |
| ------------- | ------ |
| January 2020 | 800 |
| February 2020 | 600 |
| March 2020 | 400 |
| April 2020 | 200 |
In earlier versions, starting from your existing query, you would do:
select
date_format(start_dt + interval n.n month, '%M %Y') as mon_dt,
amount * (1 - n / dur_of_months) amount
from mytable
join (
select n10.n * 10 + n1.n * 1 as n
from (
select 0 n 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
) n10
cross join (
select 0 n 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
) n1
) n on start_dt + interval n.n month <= end_dt
order by start_dt + interval n.n month
Demo
I have a table which has start and end date. Based on that table, I have to get the active orders for the past 12 months.
In end date NULL means it is active order until now.
Table Basic idea
Expected Output
Based on the past experience I'm able to write the query like this for the current year January but i need it for all the months as shown in the above picture
select
mnth.num, count(*)
from (
select 1 AS num 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
) mnth
left join (
select
o.id
, case
when o.start_date < date_format(current_date(), '%Y-01-01') then 1
else month(o.start_date)
end AS start_month
, case
when o.end_date < date_format(current_date(), '%Y-01-01') then 0
when o.end_date >= date_format(current_date(), '%Y-01-01') then month(o.end_date)
else month(current_date())
end AS end_month
from order o
) active_work_orders on mnth.num between active_work_orders.start_month and active_work_orders.end_month
where mnth.num <= month(current_date())
group by
mnth.num
;
Based on the output I have to generate a graph. Kindly help on this
Fiddle link click here
Maybe something like this
select mthnum,
count(*)
from
(
select mthnum, o.*,year(start_date)* 12 + month(start_date),year(end_date) * 12 + month(end_date)
from
(
#select 2016*12 + 11 AS mthnum union all select 2016*12 + 12 union all select 2017*12 + 1 union all select 2017*12 + 2
select 2017*100 + 2 as mthnum
union all select 2017*100 + 3 union all select 2017*100 + 4 union all select 2017*100 + 5 union all select 2017*100 + 6
union all select 2017*100 + 7 union all select 2017*100 + 8 union all select 2017*100 + 9 union all select 2017*100 + 10
union all select 2017*100 + 11 union all select 2017*100 + 12 union all select 2018*100 + 1
) mth
cross join
(select * from order_test_new #where order_num = 1288
) o
) s where (s.mthnum >= year(s.start_date)*100 + month(start_date)) and
(end_date is null or s.mthnum <= year(end_date) * 100 + month(s.end_date))
group by mthnum
result
+--------+----------+
| mthnum | count(*) |
+--------+----------+
| 201702 | 9 |
| 201703 | 9 |
| 201704 | 9 |
| 201705 | 8 |
| 201706 | 8 |
| 201707 | 8 |
| 201708 | 8 |
| 201709 | 7 |
| 201710 | 7 |
| 201711 | 6 |
| 201712 | 6 |
| 201801 | 6 |
+--------+----------+
12 rows in set (0.00 sec)
This considers that an order with and end_date is active in the end_date month.
You can take the help of extract function to extract year and month and do a group by based on that.
Then, for displaying, you can use date_format function.
So, here it goes:
SELECT
DATE_FORMAT(start_date, '%b-%y') AS `Month`, COUNT(*) AS `Active`
FROM
orders
WHERE
end_date IS NULL
GROUP BY EXTRACT(YEAR_MONTH FROM start_date)
For getting result from last 12 months only, you can use the following:
SELECT
`month_list`.`month`, IFNULL(`active_orders`.`active_count`, 0) AS `active_count`
FROM
(SELECT
*
FROM
(SELECT DATE_FORMAT(NOW(), '%b-%y') AS `month`
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 2 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 4 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 5 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 6 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 7 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 8 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 9 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 10 MONTH, '%b-%y')
UNION
SELECT DATE_FORMAT(NOW() - INTERVAL 11 MONTH, '%b-%y'))
AS `months`) as `month_list`
LEFT JOIN
(SELECT
DATE_FORMAT(start_date, '%b-%y') AS `month`, COUNT(*) AS `active_count`
FROM
`order`
WHERE
end_date IS NULL
GROUP BY EXTRACT(YEAR_MONTH FROM start_date)) as `active_orders` on `month_list`.`month` = `active_orders`.`month`;
What query to get all the Sundays date between January - March 2016?
so output just the shown date only.
| Sundays |
|- - - - - -|
| 2 |
| 9 |
| 16 |
| 23 |
| 30 |
- - - - - -
i find query like this select ('2013-04-15' - interval dayofweek('2013-08-15') - 1 day) - interval (weekofyear('2013-04-15') - 1) * 7 day as SUNDAY;
but just shown one sunday.
To get the Sunday below query will work for you
select DATE_ADD('2016-01-01', INTERVAL ROW DAY) as Date,
row+1 as DayOfMonth
from
(
SELECT #row := #row + 1 as row
FROM
(
select 0
union all
select 1
union all
select 3
union all
select 4
union all
select 5
union all
select 6
) t1,
(
select 0
union all
select 1
union all
select 3
union all
select 4
union all
select 5
union all
select 6
) t2,
(SELECT #row:=-1
) t3 limit 31
) b
where
DATE_ADD('2016-01-01', INTERVAL ROW DAY)
between '2016-01-01' and '2016-03-31'
and
DAYOFWEEK(DATE_ADD('2016-01-01', INTERVAL ROW DAY))=1;
I have searched similar problems here on stackoverflow but I could not understand how to make this work, what I'm trying to do...
So, I want to get last 7 days transactions from database and get total sales amount and also include empty rows if there is no data for some day.
What I have so far:
http://sqlfiddle.com/#!2/f4eda/6
This outputs:
| PURCHASE_DATE | AMOUNT |
|---------------|--------|
| 2014-04-25 | 19 |
| 2014-04-24 | 38 |
| 2014-04-22 | 19 |
| 2014-04-19 | 19 |
What I want:
| PURCHASE_DATE | AMOUNT |
|---------------|--------|
| 2014-04-25 | 19 |
| 2014-04-24 | 38 |
| 2014-04-23 | 0 |
| 2014-04-22 | 19 |
| 2014-04-21 | 0 |
| 2014-04-20 | 0 |
| 2014-04-19 | 19 |
Any help appreciated :)
This is not easy. I took help from this thread generate days from date range and combined it with your query.
So the idea was to get the list of dates from last 7 days then left join these dates with a static amount 0 to the query you have and then finally sum them. This could be used for any date range, just need to change them in both the queries
select
t1.purchase_date,
coalesce(SUM(t1.amount+t2.amount), 0) AS amount
from
(
select DATE_FORMAT(a.Date,'%Y-%m-%d') as purchase_date,
'0' as amount
from (
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
) a
where a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
)t1
left join
(
SELECT DATE_FORMAT(purchase_date, '%Y-%m-%d') as purchase_date,
coalesce(SUM(amount), 0) AS amount
FROM transactions
WHERE purchase_date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
AND vendor_id = 0
GROUP BY purchase_date
)t2
on t2.purchase_date = t1.purchase_date
group by t1.purchase_date
order by t1.purchase_date desc
DEMO
Simply put together a subquery with the dates you want and use left outer join:
select d.thedate, coalesce(SUM(amount), 0) AS amount
from (select date('2014-04-25') as thedate union all
select date('2014-04-24') union all
select date('2014-04-23') union all
select date('2014-04-22') union all
select date('2014-04-21') union all
select date('2014-04-20') union all
select date('2014-04-19')
) d left outer join
transactions t
on t.purchase_date = d.thedate and vendor_id = 0
GROUP BY d.thedate
ORDER BY d.thedate DESC;
This is for last 7 days;
select d.thedate, coalesce(SUM(amount), 0) AS amount
from (select DATE(NOW()) as thedate union all
select DATE(DATE_SUB( NOW(), INTERVAL 1 DAY)) union all
select DATE(DATE_SUB( NOW(), INTERVAL 2 DAY)) union all
select DATE(DATE_SUB( NOW(), INTERVAL 3 DAY)) union all
select DATE(DATE_SUB( NOW(), INTERVAL 4 DAY)) union all
select DATE(DATE_SUB( NOW(), INTERVAL 5 DAY)) union all
select DATE(DATE_SUB( NOW(), INTERVAL 6 DAY))) d left outer join
transactions t
on t.purchase_date = d.thedate and vendor_id = 0
GROUP BY d.thedate
ORDER BY d.thedate DESC;
with recursive all_dates(dt) as (
select '2014-04-19' as dt
union all
select dt + interval 1 day
from all_dates
where dt + interval 1 day <= '2014-04-25'
)
select d.dt as purchase_date, coalesce(m.amount, 0) as purchased
from all_dates as d
left join mytable m
on d.dt = m.purchase_date
order by purchase_date desc;