Mysql calculate multiple SUM() varialbe - mysql

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'

Related

MySQL - 1111 - Invalid use of group function

I'm getting an error of group function while trying to get the volatage stability per hour.
table as an image below.
table-image
SELECT ip,
SUM(CASE HOUR(time) WHEN '1' THEN CAST(AVG(IF(volt=0,0,1)) AS DECIMAL(2,1)) ELSE 0 END) AS '1',
SUM(CASE HOUR(time) WHEN '2' THEN CAST(AVG(IF(volt=0,0,1)) AS DECIMAL(2,1)) ELSE 0 END) AS '2',
SUM(CASE HOUR(time) WHEN '3' THEN CAST(AVG(IF(volt=0,0,1)) AS DECIMAL(2,1)) ELSE 0 END) AS '3'
FROM UPS_Status
WHERE time BETWEEN NOW() - INTERVAL 24 hour AND NOW()
GROUP BY ip, HOUR(time)
HOUR(time) does not belong in the GROUP BY clause. Try removing it:
SELECT
ip,
SUM(CASE HOUR(time) WHEN 1 THEN CAST(IF(volt=0, 0, 1) AS DECIMAL(2,1)) ELSE 0 END) AS `1`,
SUM(CASE HOUR(time) WHEN 2 THEN CAST(IF(volt=0, 0, 1) AS DECIMAL(2,1)) ELSE 0 END) AS `2`,
SUM(CASE HOUR(time) WHEN 3 THEN CAST(IF(volt=0, 0, 1) AS DECIMAL(2,1)) ELSE 0 END) AS `3`
FROM UPS_Status
WHERE
time BETWEEN NOW() - INTERVAL 24 hour AND NOW()
GROUP BY ip;

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

MySQL SUM Amount per month and group by caterory

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

Sum of sum in query

I have this
SELECT order_customFields.order_customFields_delivery_method,
sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
sum(case `order`.order_status when 'later' then 1 else 0 end) later,
sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31'
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY
order_customFields.order_customFields_delivery_method
Look like
How get another string in query, like 'Europe', in which the cells be summ of couriers(all 'eu')? Forexample Europe - paid 1471
you could use union and a proper finter
SELECT order_customFields.order_customFields_delivery_method,
sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
sum(case `order`.order_status when 'later' then 1 else 0 end) later,
sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31'
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY
order_customFields.order_customFields_delivery_method
UNION
SELECT 'EUROPE',
sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
sum(case `order`.order_status when 'later' then 1 else 0 end) later,
sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31'
AND order_customFields.order_customFields_delivery_method is not null
AND substr(order_customFields.order_customFields_delivery_method,1,3) ='eu_'
GROUP BY
substr(order_customFields.order_customFields_delivery_method,1,3)

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;