Mysql - Payments table by month column - mysql

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`

Related

count new users by each months in sql

i am trying to count new users recorded by each month of this year.
like
Need data month wise, which is registerd by month new users
JAn Feb Mar April May ..... Dec
1 2 4 2 5 ..... 1
through created_at date and user Id.
here is user_table
id created_at
1 2020-01-09 22:38:55
2 2020-02-09 22:38:55
3 2020-02-09 22:38:55
4 2020-03-09 22:38:55
5 2020-03-09 22:38:55
6 2020-03-09 22:38:55
7 2020-04-09 22:38:55
8 2020-04-09 22:38:55
9 2020-05-09 22:38:55
i am trying with this query
SELECT ut.id, Month(FROM_UNIXTIME(ut.created_at)), Count(*)
from $userTable ut
where FROM_UNIXTIME(ut.created_at) >= CURDATE() - INTERVAL 1 YEAR
GROUP BY Month(FROM_UNIXTIME(ut.created_at));
You can group by year and sum by month like this:
select YEAR(created_at) as year,
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 ut group by YEAR(created_at)
all columns that being used in the select that are not an aggregated function need to be grouped, ut.id is not group and in your case should be removed from the select
Try
SELECT Month(FROM_UNIXTIME(ut.created_at)), Count(*) from $userTable ut
where FROM_UNIXTIME(ut.created_at) >= CURDATE() - INTERVAL 1 YEAR
GROUP BY Month(FROM_UNIXTIME(ut.created_at));
If you want to mix data from this year and last year for the past 12 months, you can use:
SELECT SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 1) AS Jan,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 2) AS Feb,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 3) AS Mar,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 4) AS Apr,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 5) AS May,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 6) AS Jun,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 7) AS Jul,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 8) AS Aug,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 9) AS Sep,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 10) AS Oct,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 11) AS Nov,
SUM(MONTH(FROM_UNIXTIME(ut.created_at)) = 12) AS Dec
FROM ut
WHERE FROM_UNIXTIME(ut.created_at) >= CURDATE() - INTERVAL 1 YEAR
Note: This assumes that FROM_UNIXTIME() is a actually necessary. It also uses a convenient MySQL shortcut for conditional aggreation.

MYSQL Using alias as column in case when

How do I select alias as a 'date1' column value?I want to select the alias that will show month & year format like 'Jan-18'. MySQL ver 5.0.
I tried this but didnt work:
SUM(CASE WHEN MONTH(date1) = 1 THEN quantity/1000 ELSE NULL END) AS date1
My query :
SELECT
SUM(CASE WHEN MONTH(date1) = 1 THEN quantity/1000 ELSE NULL END) AS jan
,SUM(CASE WHEN MONTH(date1) = 2 THEN quantity/1000 ELSE NULL END) AS feb
,SUM(CASE WHEN MONTH(date1) = 3 THEN quantity/1000 ELSE NULL END) AS mar
,SUM(CASE WHEN MONTH(date1) = 4 THEN quantity/1000 ELSE NULL END) AS apr
,SUM(CASE WHEN MONTH(date1) = 5 THEN quantity/1000 ELSE NULL END) AS may
,SUM(CASE WHEN MONTH(date1) = 6 THEN quantity/1000 ELSE NULL END) AS jun
,SUM(CASE WHEN MONTH(date1) = 7 THEN quantity/1000 ELSE NULL END) AS jul
,SUM(CASE WHEN MONTH(date1) = 8 THEN quantity/1000 ELSE NULL END) AS aug
,SUM(CASE WHEN MONTH(date1) = 9 THEN quantity/1000 ELSE NULL END) AS sep
,SUM(CASE WHEN MONTH(date1) = 10 THEN quantity/1000 ELSE NULL END) AS octo
,SUM(CASE WHEN MONTH(date1) = 11 THEN quantity/1000 ELSE NULL END) AS nov
,SUM(CASE WHEN MONTH(date1) = 12 THEN quantity/1000 ELSE NULL END) AS dece
FROM
data2
WHERE
date1 >= DATE_SUB('2018-09-11', INTERVAL 11 MONTH) AND date1 <= DATE_FORMAT('2018-09-11', '%Y-%m-31')
GROUP BY unit
It works as follows
sum(case when expression = true then 100 else 0 end) -- this will work fine
sum(case when expression = true then 100 else null end) -- this will also work -
but gives you nulls even
if there is one row
where the expression is
false

SQL grouping: Creating monthly columns with grouped data

I have the following mySQL query which generates a table listing the individual failure data and groups by the month and frequency of different types of failures.
SELECT (CASE WHEN MONTH(runtable.time) = 1 THEN 'Jan'
WHEN MONTH(runtable.time) = 2 THEN 'FEB'
WHEN MONTH(runtable.time) = 3 THEN 'MAR'
WHEN MONTH(runtable.time) = 4 THEN 'APR'
WHEN MONTH(runtable.time) = 5 THEN 'MAY'
WHEN MONTH(runtable.time) = 6 THEN 'JUN'
WHEN MONTH(runtable.time) = 7 THEN 'JUL'
WHEN MONTH(runtable.time) = 8 THEN 'AUG'
WHEN MONTH(runtable.time) = 9 THEN 'SEP'
WHEN MONTH(runtable.time) = 10 THEN 'OCT'
WHEN MONTH(runtable.time) = 11 THEN 'NOV'
WHEN MONTH(runtable.time) = 12 THEN 'DEC'
ELSE 'Error' END) as date_month, runtable.operation, pareto.failure
FROM runtable
JOIN pareto ON pareto.run_id = runtable.id
WHERE runtable.time BETWEEN '2016-01-01 00:00:00' AND '2016-12-13 23:59:59'
AND runtable.operation IN ('d','hr')
GROUP BY MONTH(runtable.time), pareto.failure
ORDER BY MONTH(runtable.time) desc, n desc;
I would like to be able to have the months as separate columns in in the table along with the total for the year, but am unaware of how to do this. My current issue is the only way I have tried of doing this involves using sum or case for each month but then I am unable to group the count of different failure types together for each month.
The way is exactly what you described: you need to use CASE WHEN and SUM functions to solve this. Take a look at this thread:
how to select 2 table like this
By the way, you have easier ways to describe months without a case function as your example code. Take a look:
MySQL DATE_FORMAT() Function
Something like: select DATE_FORMAT(now(),'%m')
I didn't understand your problem with the error column. Be specify what problems are you run into.
Good luck!
As far as I understand you're looking for the sum(pareto.failues) by month of each operation.
Check it here: http://rextester.com/MTDK27603
SELECT
runtable.operation,
pareto.failure,
COUNT(CASE WHEN MONTH(runtable.time) = 1 THEN 1 else NULL end) as 'Jan',
COUNT(CASE WHEN MONTH(runtable.time) = 2 THEN 1 else NULL end) as 'Feb',
COUNT(CASE WHEN MONTH(runtable.time) = 3 THEN 1 else NULL end) as 'Mar',
COUNT(CASE WHEN MONTH(runtable.time) = 4 THEN 1 else NULL end) as 'Apr',
COUNT(CASE WHEN MONTH(runtable.time) = 5 THEN 1 else NULL end) as 'May',
COUNT(CASE WHEN MONTH(runtable.time) = 6 THEN 1 else NULL end) as 'Jun',
COUNT(CASE WHEN MONTH(runtable.time) = 7 THEN 1 else NULL end) as 'Jul',
COUNT(CASE WHEN MONTH(runtable.time) = 8 THEN 1 else NULL end) as 'Aug',
COUNT(CASE WHEN MONTH(runtable.time) = 9 THEN 1 else NULL end) as 'Sep',
COUNT(CASE WHEN MONTH(runtable.time) = 10 THEN 1 else NULL end) as 'Oct',
COUNT(CASE WHEN MONTH(runtable.time) = 11 THEN 1 else NULL end) as 'Nov',
COUNT(CASE WHEN MONTH(runtable.time) = 12 THEN 1 else NULL end) as 'Dec',
COUNT(pareto.failure) as 'Total year'
FROM
runtable
JOIN pareto
ON pareto.run_id = runtable.id
WHERE
runtable.time BETWEEN '2016-01-01 00:00:00' AND '2016-12-13 23:59:59'
AND runtable.operation IN ('d','hr')
GROUP BY
runtable.operation, pareto.failure
;

Get Data for Grouping all Months in a year using MySql and DB2

below is my query
SELECT DISTINCT e2.status as status, e2.year as year, e2.month as month,e2.Jan,e2.Feb,e2.Mar,e2.Apr,e2.May,e2.Jun,e2.Jul,e2.Aug,e2.Sep,e2.Oct,e2.Nov, e2.Dece, e2.countTotal FROM tranx_history e1 INNER JOIN (
SELECT YEAR(e.create_dt) AS year, MONTH(e.create_dt) AS month,
SUM(CASE WHEN MONTH(e.create_dt) = 01 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE WHEN MONTH(e.create_dt) = 02 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE WHEN MONTH(e.create_dt) = 03 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE WHEN MONTH(e.create_dt) = 04 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE WHEN MONTH(e.create_dt) = 05 THEN 1 ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(e.create_dt) = 06 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE WHEN MONTH(e.create_dt) = 07 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE WHEN MONTH(e.create_dt) = 08 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE WHEN MONTH(e.create_dt) = 09 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE WHEN MONTH(e.create_dt) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN MONTH(e.create_dt) = 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE WHEN MONTH(e.create_dt) = 12 THEN 1 ELSE 0 END) AS Dece,
SUM(CASE WHEN MONTH(e.create_dt) >= 01 AND MONTH(e.create_dt) <= 12 THEN 1 ELSE 0 END) as countTotal,
e.trnx_status as status
from tranx_history e
GROUP BY e.trnx_status, MONTH(e.create_dt),YEAR(e.create_dt) ) e2 ON YEAR(e1.create_dt) = e2.year AND MONTH(e1.create_dt) = e2.month ORDER BY e2.year asc, e2.month asc
For the Above query, The Output we are getting is first it is grouping by Jan then in the next row it is grouping by Feb even though status and year are same
i want the output Jan and Feb with same status and year in a single row not in different rows

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;