Select and create month row and count the data - MySQL - mysql

I had a problem. I have the data like below:
-----------------------
| last_login_dt |
-----------------------
| 2015-08-11 08:06:36 |
| 2015-06-10 22:06:43 |
| 2015-06-11 08:06:58 |
| 2015-09-11 08:06:45 |
-----------------------
So far, I managed to count this kind of data and turn into this using below statement:
SELECT DATE_FORMAT(last_login_dt,'%m/%Y') as `month`,
count(last_login_dt) as `total_visits` from public_user
WHERE DATE_FORMAT(last_login_dt,'%Y') = YEAR(CURDATE())
group by DATE_FORMAT(last_login_dt,'%m/%Y')
order by `month` asc;
----------------------------
| month | total_visits |
----------------------------
| 06/2015 | 2 |
| 08/2015 | 1 |
| 09/2015 | 1 |
----------------------------
The problem is, how to turn the result like below. Instead this table only have 4 rows of data, how to create a jan, feb, mar, apr ... row with total_visit = 0:
---------------------
| Month | Total |
---------------------
| Jan | 0 |
| Feb | 0 |
| Mar | 0 |
| Apr | 0 |
| May | 0 |
| Jun | 2 |
| Jul | 0 |
| Aug | 1 |
| Sep | 1 |
| Oct | 0 |
| Nov | 0 |
| Dis | 0 |
---------------------

I use your own query because it's working and I modified it a bit. Consider the query below:
SELECT `month`, `total_visits` FROM
(
SELECT * FROM
(
SELECT '01' AS `month_num`, 'Jan' As `month`, 0 AS `total_visits`
UNION
SELECT '02' AS `month_num`, 'Feb' AS `month`, 0 AS `total_visits`
UNION
SELECT '03' AS `month_num`, 'Mat' AS `month`, 0 AS `total_visits`
UNION
SELECT '04' AS `month_num`, 'Apr' AS `month`, 0 AS `total_visits`
UNION
SELECT '05' AS `month_num`, 'May' AS `month`, 0 AS `total_visits`
UNION
SELECT '06' AS `month_num`, 'Jun' AS `month`, 0 AS `total_visits`
UNION
SELECT '07' AS `month_num`, 'Jul' AS `month`, 0 AS `total_visits`
UNION
SELECT '08' AS `month_num`, 'Aug' AS `month`, 0 AS `total_visits`
UNION
SELECT '09' AS `month_num`, 'Sep' AS `month`, 0 AS `total_visits`
UNION
SELECT '10' AS `month_num`, 'Oct' AS `month`, 0 AS `total_visits`
UNION
SELECT '11' AS `month_num`, 'Nov' AS `month`, 0 AS `total_visits`
UNION
SELECT '12' AS `month_num`, 'Dec' AS `month`, 0 AS `total_visits`
UNION
SELECT DATE_FORMAT(last_login_dt,'%m') as `month_num`, DATE_FORMAT(last_login_dt,'%b') as `month`,
count(last_login_dt) as `total_visits` from public_user
WHERE DATE_FORMAT(last_login_dt,'%Y') = YEAR(CURDATE())
group by DATE_FORMAT(last_login_dt,'%m/%Y')
order by `total_visits` desc
) AS tmp
GROUP BY `month`
) AS total_visits
ORDER BY `month_num`;
It results in:
---------------------
| Month | Total |
---------------------
| Jan | 0 |
| Feb | 0 |
| Mar | 0 |
| Apr | 0 |
| May | 0 |
| Jun | 2 |
| Jul | 0 |
| Aug | 1 |
| Sep | 1 |
| Oct | 0 |
| Nov | 0 |
| Dis | 0 |
---------------------
I first created the table that displays the months and its total_visits (which is obviously 0). Then I unite your query to the derived table I created. I also added the month_num field for the sole purpose of sorting the data by month although it's not shown in the final result.

You're almost there. Try this %b in your date format string:
mysql> SELECT DATE_FORMAT(NOW(), '%b');
+--------------------------+
| DATE_FORMAT(NOW(), '%b') |
+--------------------------+
| Jun |
+--------------------------+
1 row in set (0.00 sec)
mysql>

The format for the month name is %b.
So I think you must change the %m to %b.
Try this sql if it works:
SELECT DATE_FORMAT(last_login_dt,'%b/%Y') as `month`,
count(last_login_dt) as `total_visits` from public_user
WHERE DATE_FORMAT(last_login_dt,'%Y') = YEAR(CURDATE())
group by DATE_FORMAT(last_login_dt,'%m/%Y')
order by `month` asc;
UPDATE AS OF 06/11/2015 11:57 AM GMT+8
We cannot display list of months in rows, but rather I used to make a derived column to display months and in total_visits on the row. I used case for the query to check if date is for the specific month.
SELECT
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '01' THEN count(last_login_dt) ELSE '0' END AS Jan,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '02' THEN count(last_login_dt) ELSE '0' END AS Feb,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '03' THEN count(last_login_dt) ELSE '0' END AS Mar,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '04' THEN count(last_login_dt) ELSE '0' END AS Apr,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '05' THEN count(last_login_dt) ELSE '0' END AS May,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '06' THEN count(last_login_dt) ELSE '0' END AS Jun,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '07' THEN count(last_login_dt) ELSE '0' END AS Jul,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '08' THEN count(last_login_dt) ELSE '0' END AS Aug,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '09' THEN count(last_login_dt) ELSE '0' END AS Sep,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '10' THEN count(last_login_dt) ELSE '0' END AS Oct,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '11' THEN count(last_login_dt) ELSE '0' END AS Nov,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '12' THEN count(last_login_dt) ELSE '0' END AS `Dec`
FROM public_user;
The result will be:
------------------------------------------------------------------------
| Jan | Feb | Mar| Apr| May| Jun| Jul| Aug | Sep| Oct | Nov | Dec |
------------------------------------------------------------------------
| 0 | 0 | 0 | 0 | 0 | 2 | 0 | 1 | 1 | 0 | 0 | 0 |
------------------------------------------------------------------------

Related

How to get the transposition of a table in mysql [duplicate]

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 9 years ago.
i have problem with transposing row to column and column to row.
I can do that if it just transpose row to column or column to row.
This my table with data
UNIT|JAN|FEB|MAR|APR|MEI|JUN
CS-1|100|200|300|400|500|600
CS-2|111|222|333|444|555|666
CS-3|331|123|423|923|918|123
and I would like to get the following output
MONTH|CS-1|CS-2|CS-3
JAN |100 |111 |331
FEB |200 |222 |123
MAR |300 |333 |423
etc..
Anybody know how to do this? Thanks very much!
You can do it this way
SELECT month,
MAX(CASE WHEN unit = 'CS-1' THEN value END) `CS-1`,
MAX(CASE WHEN unit = 'CS-2' THEN value END) `CS-2`,
MAX(CASE WHEN unit = 'CS-3' THEN value END) `CS-3`
FROM
(
SELECT unit, month,
CASE month
WHEN 'JAN' THEN jan
WHEN 'FEB' THEN feb
WHEN 'MAR' THEN mar
WHEN 'APR' THEN apr
WHEN 'MAY' THEN may
WHEN 'JUN' THEN jun
END value
FROM table1 t CROSS JOIN
(
SELECT 'JAN' month UNION ALL
SELECT 'FEB' UNION ALL
SELECT 'MAR' UNION ALL
SELECT 'APR' UNION ALL
SELECT 'MAY' UNION ALL
SELECT 'JUN'
) c
) q
GROUP BY month
ORDER BY FIELD(month, 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN')
Output:
| MONTH | CS-1 | CS-2 | CS-3 |
|-------|------|------|------|
| JAN | 100 | 111 | 331 |
| FEB | 200 | 222 | 123 |
| MAR | 300 | 333 | 423 |
| APR | 400 | 444 | 923 |
| MAY | 500 | 555 | 918 |
| JUN | 600 | 666 | 123 |
Here is SQLFiddle demo

Mysql: Fill column with consecutive numbers of days in a month

I have this table: "sales"
+-------------+---------+
| date | total |
+-------------+---------+
| 2018-12-04 | 269.10 |
| 2018-12-05 | 29.00 |
| 2018-12-06 | 107.10 |
| 2018-12-06 | 34.00 |
| 2018-12-08 | 69.50 |
| 2018-12-08 | 223.00 |
| 2018-12-08 | 68.00 |
| 2018-12-09 | 99.00 |
| 2018-12-10 | 59.50 |
| ... | ... |
+-------------+---------+
I'm trying this query
SELECT DAY(date) AS Days,
SUM(CASE WHEN MONTH(date) = 12 THEN total ELSE NULL END) AS December
FROM sales WHERE YEAR(date) = 2018 GROUP BY date
And I get
+-------+----------+
| Days | December |
+-------+----------+
| 4 | 269.10 |
| 5 | 29.00 |
| 6 | 141.10 |
| 8 | 360.50 |
| 9 | 99.00 |
| 10 | 59.50 |
| ... | ... |
+-------+----------+
But I want consecutive days like this:
+-------+----------+
| Days | December |
+-------+----------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | 269.10 |
| 5 | 29.00 |
| 6 | 141.10 |
| 7 | NULL |
| 8 | 360.50 |
| 9 | 99.00 |
| 10 | 59.50 |
| ... | ... |
| 31 | 123.00 |
+-------+----------+
Can you help me plss..
PS: I have several months and years in "date" column from "sales" table.
This recursive CTE generates a list of dates corresponding to the month and year specified in the doi CTE, and then LEFT JOINs that to the sales table to get the sales for that month. It will work for any month/year, just change the values in the doi CTE, and the title of the SUM column (currently December) to suit.
WITH RECURSIVE doi AS (
SELECT 12 AS month,
2018 AS year
),
cte AS (
SELECT DATE(CONCAT_WS('-', year, month, 1)) AS date
FROM doi
UNION ALL
SELECT date + INTERVAL 1 DAY
FROM cte
WHERE date < LAST_DAY(date)
)
SELECT DAY(cte.date) AS Days,
ROUND(SUM(s.total),2) AS December
FROM cte
LEFT JOIN sales s ON s.date = cte.date
GROUP BY cte.date
ORDER BY cte.date
Output is too long to show here but can be seen at this demo on dbfiddle
Update
To expand this query to cover an entire year requires changing the approach slightly in terms of generating an entire year's worth of dates, and then using conditional aggregation to get the sums for each day of each month:
WITH RECURSIVE doi AS (
SELECT 2018 AS year
),
cte AS (
SELECT DATE(CONCAT_WS('-', year, 1, 1)) AS date
FROM doi
UNION ALL
SELECT date + INTERVAL 1 DAY
FROM cte
CROSS JOIN doi
WHERE date < DATE(CONCAT_WS('-', doi.year, 12, 31))
)
SELECT DAY(cte.date) AS Days,
ROUND(SUM(CASE WHEN MONTH(s.date) = 1 THEN s.total END),2) AS January,
ROUND(SUM(CASE WHEN MONTH(s.date) = 2 THEN s.total END),2) AS February,
ROUND(SUM(CASE WHEN MONTH(s.date) = 3 THEN s.total END),2) AS March,
ROUND(SUM(CASE WHEN MONTH(s.date) = 4 THEN s.total END),2) AS April,
ROUND(SUM(CASE WHEN MONTH(s.date) = 5 THEN s.total END),2) AS May,
ROUND(SUM(CASE WHEN MONTH(s.date) = 6 THEN s.total END),2) AS June,
ROUND(SUM(CASE WHEN MONTH(s.date) = 7 THEN s.total END),2) AS July,
ROUND(SUM(CASE WHEN MONTH(s.date) = 8 THEN s.total END),2) AS August,
ROUND(SUM(CASE WHEN MONTH(s.date) = 9 THEN s.total END),2) AS September,
ROUND(SUM(CASE WHEN MONTH(s.date) = 10 THEN s.total END),2) AS October,
ROUND(SUM(CASE WHEN MONTH(s.date) = 11 THEN s.total END),2) AS November,
ROUND(SUM(CASE WHEN MONTH(s.date) = 12 THEN s.total END),2) AS December
FROM cte
LEFT JOIN sales s ON s.date = cte.date
GROUP BY DAY(cte.date)
ORDER BY DAY(cte.date)
Demo on dbfiddle
generate your months using union and do right join
select t1.d as Days
, sum(iif(month(date) = 12, total, null) as December
from sales
right join (select 1 as d
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
.... ) as t1 on t1.d = day(date)
where year(date) = 2012
group by date
if you are using mysql v8.0, you can use recursive queries.
with recursive cte as(
select 1 as d
union all
select d + 1 from cte where d < day(last_day('2019-12-01'))
)
select coalesce(day(s.date), t1.d) as Days
, sum(iif(month(s.date) = 12, total, null) as December
from sales s
right join cte as t1 on t1.d = day(s.date)
where year(date) = 2012
group by coalesce(day(s.date), t1.d)

Merge two complex queries of SQL into single

I have 3 tables, one of the customer's, product_type_1 and product_type_2.
Customers
| id | name |
|----|-------|
| 1 | Alex |
| 2 | John |
| 3 | Ahmad |
| 4 | Sam |
product_type_1
| id | order_by | Date
|----|--------------|-------|
| 1 |------ 1 ---- | 2019-03-01
| 2 |------ 2 ----| 2019-03-02
| 3 |------ 2 ----| 2019-03-03
| 4 |------ 3 ----| 2019-03-04
product_type_2
| id | order_by | Date
|----|--------------|-------|
| 1 |------ 1 ---- | 2019-03-01
| 2 |------ 3 ----| 2019-03-02
| 3 |------ 3 ----| 2019-03-03
| 4 |------ 2 ----| 2019-03-04
The final output will be the sum of amount of both product type grouped by name of the customer of each month of a specific year. I have written query but it works for 1 product type at a time. But I want sum of both i.e:
Customer | Jan | Feb | Mar .... Total<br>
:------------------------------------------------------:
John ------ | 0 -- |--- 0 |--- 3 ...... 3
As John ordered total 3 products in 2019.
The query is
select c.name,
sum( month(o.order_date) = 1 and year(o.order_date)=2010) as Jan,
sum( month(o.order_date) = 2 and year(o.order_date)=2010) as Feb,
sum( month(o.order_date) = 3 and year(o.order_date)=2010) as Mar,
sum( month(o.order_date) = 4 and year(o.order_date)=2010) as Apr,
sum( month(o.order_date) = 5 and year(o.order_date)=2010) as May,
sum( month(o.order_date) = 6 and year(o.order_date)=2010) as Jun,
sum( month(o.order_date) = 7 and year(o.order_date)=2010) as Jul,
sum( month(o.order_date) = 8 and year(o.order_date)=2010) as Aug,
sum( month(o.order_date) = 9 and year(o.order_date)=2010) as Sep,
sum( month(o.order_date) = 10 and year(o.order_date)=2010) as Oct,
sum( month(o.order_date) = 11 and year(o.order_date)=2010) as Nov,
sum( month(o.order_date) = 12 and year(o.order_date)=2010) as December,
count(*) as total
from customers c join
(
select order_by as cID, order_price , order_date
from orders where year(order_date)=2010
) o
on o.cID = c.id and o.order_price > 0
group by c.name
order by total desc
Use union all and aggregation:
select c.id, c.name,
sum( month(o.order_date) = 1 and year(o.order_date)=2010) as Jan,
. . .
from customers c left join
((select order_by, date
from product_type_1
) union all
(select order_by, date
from product_type_2
)
) p12
on p12.order_by = c.id
group by c.id, c.name

query to find closest lesser date

I have a table with lunch effective date and its rate.
I need to display rate from its nearest lesser effective date (created_on) for each date column.
lunch_rate table:
created_on | rate
-----------+-------
2018-06-01 | 30
2018-06-04 | 60
Here's what I tried to do:
SELECT userId,
SUM(CASE WHEN date= '2018-06-01' AND lunchStatus = 1 THEN (SELECT MAX(rate) FROM lunch_rate WHERE DATE(created_on) <= date LIMIT 1) ELSE 0 END) '2018-06-01',
SUM(CASE WHEN date= '2018-06-02' AND lunchStatus = 1 THEN (SELECT MAX(rate) FROM lunch_rate WHERE DATE(created_on) <= date LIMIT 1) ELSE 0 END) '2018-06-02',
SUM(CASE WHEN date= '2018-06-03' AND lunchStatus = 1 THEN (SELECT MAX(rate) FROM lunch_rate WHERE DATE(created_on) <= date LIMIT 1) ELSE 0 END) '2018-06-03',
SUM(CASE WHEN date= '2018-06-04' AND lunchStatus = 1 THEN (SELECT MAX(rate) FROM lunch_rate WHERE DATE(created_on) <= date LIMIT 1) ELSE 0 END) '2018-06-04'
FROM
(
SELECT userId, lunchStatus, DATE(issuedDateTime) as date
FROM `lunch_status`
WHERE DATE(issuedDateTime) BETWEEN '2018-06-01' AND '2018-06-04'
) as a
GROUP BY userId;
But this query only gives maximum rate of all, without considering the nearest effective date.
Here's the outcome:
userId | 2018-06-01 | 2018-06-02 | 2018-06-03 | 2018-06-04
------------------------------------------------------------------------
131 | 60 | 60 | 0 | 60
132 | 60 | 60 | 60 | 0
133 | 0 | 0 | 0 | 60
134 | 0 | 0 | 0 | 60
Expected outcome:
userId | 2018-06-01 | 2018-06-02 | 2018-06-03 | 2018-06-04
------------------------------------------------------------------------
131 | 30 | 30 | 0 | 60
132 | 30 | 30 | 30 | 0
133 | 0 | 0 | 0 | 60
134 | 0 | 0 | 0 | 60
SUM(CASE WHEN ... THEN (SELECT MAX(rate) FROM lunch_rate WHERE DATE(created_on) <= date LIMIT 1) ELSE 0 END) ....',
How can I select lunch rate that was effective on that date?
If I understand correctly, you want the calculation in the subquery:
SELECT userId,
SUM(CASE WHEN date = '2018-06-01' AND lunchStatus = 1
THEN rate ELSE 0
END) as `2018-06-01`,
SUM(CASE WHEN date = '2018-06-02' AND lunchStatus = 1
THEN rate ELSE 0
END) as `2018-06-02`,
SUM(CASE WHEN date = '2018-06-03' AND lunchStatus = 1
THEN rate ELSE 0
END) as `2018-06-03`,
SUM(CASE WHEN date = '2018-06-04' AND lunchStatus = 1
THEN rate ELSE 0
END) as `2018-06-04`
FROM (SELECT ls.*, DATE(ls.issuedDateTime) as date
(SELECT lr.rate
FROM lunch_rate lr
WHERE DATE(lr.created_on) <= DATE(ls.issuedDateTime)
ORDER BY lr.created_on DESC
LIMIT 1
) as rate
FROM lunch_status ls
WHERE DATE(issuedDateTime) BETWEEN '2018-06-01' AND '2018-06-04'
) lr
GROUP BY lr.userId;
Note the other changes:
The subquery for lunch_rate does not use MAX(). Instead, it uses ORDER BY.
The column aliases are surrounded by backticks, not single quotes. I don't approve of the names (because they need to be escaped). But if you want them, use proper escape characters.
The tables are given reasonable aliases and column names are qualified.
You could try something like this:
SELECT lr1.rate
FROM lunch_rate lr1
WHERE lr1.created_on <= my_date
AND NOT EXISTS (SELECT *
FROM lunch_rate lr2
WHERE lr2.created_on > lr1.created_on
AND lr2.created_on <= my_date);

MySQL monthly Sale of last 12 months including months with no Sale

SELECT DATE_FORMAT(date, "%b") AS month, SUM(total_price) as total
FROM cart
WHERE date <= NOW()
and date >= Date_add(Now(),interval - 12 month)
GROUP BY DATE_FORMAT(date, "%m-%Y")
This query displaying result for only existing month. I need all 12 months sales.
Output:
"month" "total"
--------------
"Jun" "22"
"Aug" "30"
"Oct" "19"
"Nov" "123"
"Dec" "410"
Required Output:
"month" "total"
--------------
"Jan" "0"
"Feb" "0"
"Mar" "0"
"Apr" "0"
"May" "0"
"Jun" "22"
"Jul" "0"
"Aug" "30"
"Sep" "0"
"Oct" "19"
"Nov" "123"
"Dec" "410"
Consider the following table
mysql> select * from cart ;
+------+------------+-------------+
| id | date | total_price |
+------+------------+-------------+
| 1 | 2014-01-01 | 10 |
| 2 | 2014-01-20 | 20 |
| 3 | 2014-02-03 | 30 |
| 4 | 2014-02-28 | 40 |
| 5 | 2014-06-01 | 50 |
| 6 | 2014-06-13 | 24 |
| 7 | 2014-12-12 | 45 |
| 8 | 2014-12-18 | 10 |
+------+------------+-------------+
Now as per the logic you are looking back one year and december will appear twice in the result i.e. dec 2013 and dec 2014 and if we need to have a separate count for them then we can use the following technique of generating dynamic date range MySql Single Table, Select last 7 days and include empty rows
t1.month,
t1.md,
coalesce(SUM(t1.amount+t2.amount), 0) AS total
from
(
select DATE_FORMAT(a.Date,"%b") as month,
DATE_FORMAT(a.Date, "%m-%Y") as md,
'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 <= NOW() and a.Date >= Date_add(Now(),interval - 12 month)
group by md
)t1
left join
(
SELECT DATE_FORMAT(date, "%b") AS month, SUM(total_price) as amount ,DATE_FORMAT(date, "%m-%Y") as md
FROM cart
where Date <= NOW() and Date >= Date_add(Now(),interval - 12 month)
GROUP BY md
)t2
on t2.md = t1.md
group by t1.md
order by t1.md
;
Output will be
+-------+---------+-------+
| month | md | total |
+-------+---------+-------+
| Jan | 01-2014 | 30 |
| Feb | 02-2014 | 70 |
| Mar | 03-2014 | 0 |
| Apr | 04-2014 | 0 |
| May | 05-2014 | 0 |
| Jun | 06-2014 | 74 |
| Jul | 07-2014 | 0 |
| Aug | 08-2014 | 0 |
| Sep | 09-2014 | 0 |
| Oct | 10-2014 | 0 |
| Nov | 11-2014 | 0 |
| Dec | 12-2013 | 0 |
| Dec | 12-2014 | 55 |
+-------+---------+-------+
13 rows in set (0.00 sec)
And if you do not care about the above case i.e. dec 2014 and dec 2013
Then just change the group by in dynamic date part as
where a.Date <= NOW() and a.Date >= Date_add(Now(),interval - 12 month)
group by month
and final group by as group by t1.month
Thanks for #pankaj hint, Here i resolved it via this query...
SELECT
SUM(IF(month = 'Jan', total, 0)) AS 'Jan',
SUM(IF(month = 'Feb', total, 0)) AS 'Feb',
SUM(IF(month = 'Mar', total, 0)) AS 'Mar',
SUM(IF(month = 'Apr', total, 0)) AS 'Apr',
SUM(IF(month = 'May', total, 0)) AS 'May',
SUM(IF(month = 'Jun', total, 0)) AS 'Jun',
SUM(IF(month = 'Jul', total, 0)) AS 'Jul',
SUM(IF(month = 'Aug', total, 0)) AS 'Aug',
SUM(IF(month = 'Sep', total, 0)) AS 'Sep',
SUM(IF(month = 'Oct', total, 0)) AS 'Oct',
SUM(IF(month = 'Nov', total, 0)) AS 'Nov',
SUM(IF(month = 'Dec', total, 0)) AS 'Dec',
SUM(total) AS total_yearly
FROM (
SELECT DATE_FORMAT(date, "%b") AS month, SUM(total_price) as total
FROM cart
WHERE date <= NOW() and date >= Date_add(Now(),interval - 12 month)
GROUP BY DATE_FORMAT(date, "%m-%Y")) as sub
Month wise sale
Use Count to count month wise data.
SELECT DATE_FORMAT(date, "%b") AS month, COUNT(total_price) as total
FROM cart
WHERE date <= NOW()
and date >= Date_add(Now(),interval - 12 month)
GROUP BY DATE_FORMAT(date, "%m-%Y")