MySQL SUM Amount per month and group by caterory - mysql

I have table:
User nvarchar(30)
Date datetime
Total decimal(8,2)
And i need to get data per month like this:
I can get it this way (without month columns):
SELECT User, MONTHNAME(FROM_UNIXTIME(Date)) as month, SUM(Total) as total FROM `wp_banking_expenses` group by month,User
But i need it to make as on image above...

You can try to use condition aggregate function.
SELECT User,
SUM(CASE WHEN MONTHNAME(Date) = 'January' THEN Total END) as Jan,
SUM(CASE WHEN MONTHNAME(Date) = 'February' THEN Total END) as Feb,
SUM(CASE WHEN MONTHNAME(Date) = 'March' THEN Total END) as Mar,
SUM(CASE WHEN MONTHNAME(Date) = 'April' THEN Total END) as Apr,
SUM(CASE WHEN MONTHNAME(Date) = 'May' THEN Total END) as May,
SUM(CASE WHEN MONTHNAME(Date) = 'June' THEN Total END) as JUN,
SUM(CASE WHEN MONTHNAME(Date) = 'July' THEN Total END) as JUL,
SUM(CASE WHEN MONTHNAME(Date) = 'August' THEN Total END) as AUG,
SUM(CASE WHEN MONTHNAME(Date) = 'September' THEN Total END) as SEP,
SUM(CASE WHEN MONTHNAME(Date) = 'October' THEN Total END) as OCT,
SUM(CASE WHEN MONTHNAME(Date) = 'November' THEN Total END) as NOV,
SUM(CASE WHEN MONTHNAME(Date) = 'December' THEN Total END) as DEC
FROM `wp_banking_expenses`
group by User

use conditional aggregation using case when expression
select user,max(case when month='January' then total end) as Jan,
max(case when month='February' then total end) as Feb,
max(case when month='March' then total end) as Mar,
max(case when month='April' then total end) as Apr,
max(case when month='May' then total end) as May,
max(case when month='June' then total end) as June,
----
from
(
SELECT User, MONTHNAME(Date) as month, SUM(Total) as total
FROM `wp_banking_expenses`
group by month,User
)A group by user

Related

Mysql - Payments table by month column

I'm struggling - I have a table payments
Date
Ref
1.1.22
1
1.2.22
2
1.3.22
1
1.4.22
3
1.3.22
2
1.2.22
3
and a table of ref
Name
Ref
jo
1
Steve
2
Chris
3
I'm trying to get my sql to state
Name
Jan
Feb
Mar
Apr
Jo
Paid
Not
Paid
Not
Chris
Not
Paid
Not
Paid
Etc, so far I can but each name has one line for each month, when I want them in one row
`SELECT m.memID,m.Pay_Ref, p.ref,
(case when MONTHNAME(p.DATE) = 'January' then 'Paid' else 'Not' end) as January,
(case when MONTHNAME(p.DATE) = 'February' then 'Paid' else 'Not' end) as February,
(case when MONTHNAME(p.DATE) = 'March' then 'Paid' else 'Not' end) as March,
(case when MONTHNAME(p.DATE) = 'April' then 'Paid' else 'Not' end) as April,
(case when MONTHNAME(p.DATE) = 'May' then 'Paid' else 'Not' end) as May,
(case when MONTHNAME(p.DATE) = 'June' then 'Paid' else 'Not' end) as June,
(case when MONTHNAME(p.DATE) = 'July' then 'Paid' else 'Not' end) as July,
(case when MONTHNAME(p.DATE) = 'August' then 'Paid' else 'Not' end) as August,
(case when MONTHNAME(p.DATE) = 'September' then 'Paid' else 'Not' end) as September,
(case when MONTHNAME(p.DATE) = 'October' then 'Paid' else 'Not' end) as October,
(case when MONTHNAME(p.DATE) = 'November' then 'Paid' else 'Not' end) as November,
(case when MONTHNAME(p.DATE) = 'December' then 'Paid' else 'Not' end) as December
FROM Members AS m
INNER JOIN AllPayments AS p ON m.Pay_Ref = p.ref`

Mysql calculate multiple SUM() varialbe

I want to calculate 2 sum() i have tried query as below it works but very slowly. Any ideas to make it better?
SELECT customer,
SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31'
THEN pax+free
ELSE 0
END) AS January,
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-31'
THEN pax+free
ELSE 0
END) AS February,
( SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31'
THEN pax+free
ELSE 0
END) +
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-31'
THEN pax+free
ELSE 0
END) ) AS total
FROM rezervations
How can i make simplier like January + February as total
keep BETWEEN '2020-01-01' AND '2020-02-29' in where clause.
make sure there is index on book_day column
SELECT customer,
SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31' THEN pax+free ELSE 0 END) as January,
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-29' THEN pax+free ELSE 0 END) as February,
(SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-02-29' THEN pax+free ELSE 0 END) ) as total
FROM rezervations
WHERE
book_day BETWEEN '2020-01-01' AND '2020-02-29'

MYSQL Daily Data Compare Last Week vs Two Weeks Ago

I have daily data for multiple locations that we need to compare last week total sales with total sales 2 weeks ago. Here is the MYSQL code
SELECT
name,
SUM(CASE WHEN WEEK(date) = WEEK(now())-1 THEN sales ELSE NULL END) as 'Last Week',
SUM(CASE WHEN WEEK(date) = WEEK(now())-2 THEN sales ELSE NULL END) as '2 Weeks Ago',
SUM('1week' - '2week') AS 'Change'
FROM daily_sales
GROUP BY name
The output for the 1st three columns is correct, but I cannot get the 4th column "change" to calculate correctly. Tried SUM of the cases but could not get over invalid use og group function:
SUM((SUM(CASE WHEN WEEK(date) = WEEK(now())-1 THEN sales ELSE NULL END))
- (SUM(CASE WHEN WEEK(date) = WEEK(now())-2 THEN sales ELSE NULL END))) AS 'change'
Help!
You're almost there; just take the formula that give cols 2 and 3 and put a minus between them:
SELECT
name,
SUM(CASE WHEN WEEK(date) = WEEK(now())-1 THEN sales END) as LastWeek,
SUM(CASE WHEN WEEK(date) = WEEK(now())-2 THEN sales END) as TwoWeeksAgo,
SUM(CASE WHEN WEEK(date) = WEEK(now())-1 THEN sales END) - SUM(CASE WHEN WEEK(date) = WEEK(now())-2 THEN sales END) as change
FROM daily_sales
WHERE date >= date_sub(now(), interval 3 week)
GROUP BY name
Or by use of a subquery:
SELECT name, lastweek, twoweeksago, lastweek - twoweeksago as change
FROM
(
SELECT
name,
SUM(CASE WHEN WEEK(date) = WEEK(now())-1 THEN sales END) as LastWeek,
SUM(CASE WHEN WEEK(date) = WEEK(now())-2 THEN sales END) as TwoWeeksAgo
FROM daily_sales
WHERE date >= date_sub(now(), interval 3 week)
GROUP BY name
) x
Or by use of a cte:
WITH x AS(
SELECT
name,
SUM(CASE WHEN WEEK(date) = WEEK(now())-1 THEN sales END) as LastWeek,
SUM(CASE WHEN WEEK(date) = WEEK(now())-2 THEN sales END) as TwoWeeksAgo
FROM daily_sales
WHERE date >= date_sub(now(), interval 3 week)
GROUP BY name
)
SELECT name, lastweek, twoweeksago, lastweek - twoweeksago as change

SQL Group by "Folder Name" by month and then total

My new query is giving me few errors:
Error: ORA-0093: SQL command not properly ended.
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month (a.sent_date)=1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date)=2 then a.total_sent else 0 end) as February,
sum(case when month(a.sent_date)=3 then a.total_sent else 0 end) as March,
sum(case when month(a.sent_date)=4 then a.total_sent else 0 end) as April,
sum(case when month(a.sent_date)=5 then a.total_sent else 0 end) as May,
sum(case when month(a.sent_date)=6 then a.total_sent else 0 end) as June,
sum(case when month(a.sent_date)=7 then a.total_sent else 0 end) as July,
sum(case when month(a.sent_date)=8 then a.total_sent else 0 end) as August,
sum(case when month(a.sent_date)=9 then a.total_sent else 0 end) as September,
sum(case when month(a.sent_date)=10 then a.total_sent else 0 end) as October,
sum(case when month(a.sent_date)=11 then a.total_sent else 0 end) as November,
sum(case when month(a.sent_date)=12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id=123 AND
a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;
=========
This is my first time posting here and also a beginner at queries.
I am running a query which returns various folder names for all days. I want to group by the folder name and do a sum of the totals for each folder name by months and then a total of each column at the bottom. This is the query I am running:
select a.group_name, a.sent_date, a.total_sent
from
c_group a
where
a.partner_id=123
and a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
Displays as follows:
GROUP_NAME SENT_DATE TOTAL_SENT
Group A 1-Jan-12 37
Group B 3-Jan-12 25
Group C 1-May-12 10
Group D 1-May-12 8
Group D 1-Jan-12 11
Group A 1-Dec-12 9
I need the results to display as:
January February March April May June July August September October November December
Group A
Group B
Group C
Group D
...
....
...
....
Total Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above
You want to combine conditional aggregation with rollup:
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month(a.sent_date) = 1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date) = 2 then a.total_sent else 0 end) as February,
. . .
sum(case when month(a.sent_date) = 12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id = 123 AND
a.sent_date >= '01-JAN-2012' AND
a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;

How to count each month without repeating the lines?

I'm trying to create all months in a query instead of use several lines.
Here is the information:
CREATE TABLE quotes (
id INT,
created_at DATE,
num_policy INT);
INSERT INTO quotes VALUES
( 1, '2014-01-01',1234),
( 2, '2014-02-06',5678),
( 3, '2013-04-24',9123),
( 4, '2013-05-24',4567),
( 5, '2014-06-20',8912);
I tried this query: http://sqlfiddle.com/#!2/2f4f51/5
SET #year := 2014;
SELECT count(*) AS jan FROM `quotes` WHERE (created_at BETWEEN CONCAT( #year, "-01-01") AND CONCAT( #year, "-01-31")) ;
SELECT count(*) AS feb FROM `quotes` WHERE (created_at BETWEEN CONCAT( #year, "-02-01") AND CONCAT( #year, "-02-29")) ;
......
SELECT count(*) AS dem FROM `quotes` WHERE (created_at BETWEEN CONCAT( #year, "-12-01") AND CONCAT( #year, "-12-31")) ;
I'm trying to show all months in one line
|jan| |feb| |mar| |apr| ......
1 1 0 0
Please somebody can help me?
I will appreciate all kind of help.
This could do the magic:
SELECT CASE WHEN MonthSET #year := 2014;
SELECT SUM(CASE WHEN Month(created_at) = 1 THEN 1 ELSE 0 END) AS jan,
SUM(CASE WHEN Month(created_at) = 2 THEN 1 ELSE 0 END) AS feb,
SUM(CASE WHEN Month(created_at) = 3 THEN 1 ELSE 0 END) AS mar,
SUM(CASE WHEN Month(created_at) = 4 THEN 1 ELSE 0 END) AS apr,
SUM(CASE WHEN Month(created_at) = 5 THEN 1 ELSE 0 END) AS may,
SUM(CASE WHEN Month(created_at) = 6 THEN 1 ELSE 0 END) AS jun,
SUM(CASE WHEN Month(created_at) = 7 THEN 1 ELSE 0 END) AS jul,
SUM(CASE WHEN Month(created_at) = 8 THEN 1 ELSE 0 END) AS aug,
SUM(CASE WHEN Month(created_at) = 9 THEN 1 ELSE 0 END) AS sep,
SUM(CASE WHEN Month(created_at) = 10 THEN 1 ELSE 0 END) AS oct,
SUM(CASE WHEN Month(created_at) = 11 THEN 1 ELSE 0 END) AS nov,
SUM(CASE WHEN Month(created_at) = 12 THEN 1 ELSE 0 END) AS `dec`
FROM `quotes`
WHERE YEAR(created_at) = #year
Link to a SQL Fiddle:
http://sqlfiddle.com/#!2/2f4f51/13/1
How about using group by?
SELECT month(created_at), count(*) AS cnt
FROM `quotes`
WHERE (created_at BETWEEN CONCAT( #year, "-01-01") AND CONCAT( #year, "-01-31"))
GROUP BY month(created_at)
ORDER BY 1;
This does produce the values on separate lines. To get them on one line, use conditional aggregation:
SELECT SUM(month(created_at) = 1) as jan,
SUM(month(created_at) = 2) as feb,
. . .
FROM quotes
;WHERE (created_at BETWEEN CONCAT( #year, '-01-01') AND CONCAT( #year, '-01-31'));