MySQL - get the sum of amount every month within a fiscal year - mysql

can someone teach me or help me to figure out how to display amount within a fiscal year?. The fiscal year I am using is November to October. So let say Fiscal Year 2016 is from November 2015 to October 2016.
Now my problem is I can only display the amount per month within the chosen year. How can I do this in mysql?
This is the query I have tried, but it works only within a year.
select a.account, a.region, sum(n.amount) as 'Total Net',
sum(case when n.savings_date between date_format(n.savings_date, '%Y-11-01') and last_day(date_format(savings_date, '%Y-11-01')) then n.amount end) as `Nov`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-12-01') and last_day(date_format(savings_date, '%Y-12-01')) then n.amount end) as `Dec`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-01-01') and last_day(date_format(savings_date, '%Y-01-01')) then n.amount end) as `Jan`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-02-01') and last_day(date_format(savings_date, '%Y-02-01')) then n.amount end) as `Feb`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-03-01') and last_day(date_format(savings_date, '%Y-03-01')) then n.amount end) as `Mar`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-04-01') and last_day(date_format(savings_date, '%Y-04-01')) then n.amount end) as `Apr`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-05-01') and last_day(date_format(savings_date, '%Y-05-01')) then n.amount end) as `May`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-06-01') and last_day(date_format(savings_date, '%Y-06-01')) then n.amount end) as `Jun`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-07-01') and last_day(date_format(savings_date, '%Y-07-01')) then n.amount end) as `Jul`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-08-01') and last_day(date_format(savings_date, '%Y-08-01')) then n.amount end) as `Aug`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-09-01') and last_day(date_format(savings_date, '%Y-09-01')) then n.amount end) as `Sep`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-10-01') and last_day(date_format(savings_date, '%Y-10-01')) then n.amount end) as `Oct`
from
net_savings n left join accounts a
on a.id = n.account_id
where year(n.savings_date) = '2016'
group by a.account
order by a.account desc
I am needing this kind of output also:
Appreciate your answers or suggestions! :)

Try this
SELECT
a.account,
a.region,
YEAR(n.savings_date),
MONTH(n.savings_date),
SUM(n.amount)
FROM
net_savings n
LEFT JOIN
accounts a
ON
a.id = n.account_id
WHERE
n.savings_date >= '2015-11-01' and n.savings_date < '2016-09-01'
GROUP BY
a.account,
a.region,
YEAR(n.savings_date),
MONTH(n.savings_date),
ORDER BY
a.account DESC

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

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)