How do I get the total amount per month of records from a field in a table.
I have 2 tables, customer and activity, within activity have cost (double) field and as foreign key customer id, want to show the customer and the sum of the cost given for a date.
I made the query to sum the cost value when the date is 2016-01, in this case January I want to show every month, January, February, March ....
select idcliente,
nombre,
sum(costo) as costo
from actividad
inner join cliente
on actividad.cliente_idcliente = cliente.idcliente
where cliente_idcliente = 82
group by fecha=2016-01;
and that this consultation applies to all my clients not just one
EXAMPLE: i want show a table like this http://imgur.com/a/6jYlB
use month function
select
YEAR(fecha) as Anno
MONTH(fecha) as Mes,
min(idcliente),
min(nombre),
sum(costo) as costo
from actividad
inner join cliente on actividad.cliente_idcliente=cliente.idcliente
where cliente_idcliente=82
group by year(fecha), month(fecha)
It looks like you want a pivot query result, with the 12 months in a year being the columns, and each record corresponding to a given customer. Try the following query:
SELECT idcliente,
SUM(CASE WHEN MONTH(fecha) = 1 THEN costo ELSE 0 END) AS January,
SUM(CASE WHEN MONTH(fecha) = 2 THEN costo ELSE 0 END) AS February,
SUM(CASE WHEN MONTH(fecha) = 3 THEN costo ELSE 0 END) AS March,
SUM(CASE WHEN MONTH(fecha) = 4 THEN costo ELSE 0 END) AS April,
SUM(CASE WHEN MONTH(fecha) = 5 THEN costo ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(fecha) = 6 THEN costo ELSE 0 END) AS June,
SUM(CASE WHEN MONTH(fecha) = 7 THEN costo ELSE 0 END) AS July,
SUM(CASE WHEN MONTH(fecha) = 8 THEN costo ELSE 0 END) AS August,
SUM(CASE WHEN MONTH(fecha) = 9 THEN costo ELSE 0 END) AS September,
SUM(CASE WHEN MONTH(fecha) = 10 THEN costo ELSE 0 END) AS October,
SUM(CASE WHEN MONTH(fecha) = 11 THEN costo ELSE 0 END) AS November,
SUM(CASE WHEN MONTH(fecha) = 12 THEN costo ELSE 0 END) AS December
FROM actividad
INNER JOIN cliente
ON actividad.cliente_idcliente = cliente.idcliente
WHERE cliente_idcliente = 82 AND -- remove this to see monthly summary for all customers
YEAR(fecha) = 2016 -- change this to whatever year you want to see
GROUP BY idcliente
Related
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`
I have 70+ years of hourly visibility data that I have already calculated into a monthly average. I now need to produce seasonal averages of DJF, MAM, JJA, SON (seasonal average meaning DEC, JAN, FEB as one average and MAR, APR, MAY, the next and so on) for each of those years. Does anyone have any suggestions as to how to go about this using MySQL Workbench?
I essentially need 4 separate columns of the above mentioned months visibility. Is there something I can do under the GROUP BY function like grouping by a set or anything else?
This is what I have used to get the monthly averages but I'm not sure how to
SELECT date, avg(lowest_visibility) FROM b16.hourly
GROUP BY year(date), month(date)
ORDER BY year(date), month(date) asc
Thanks in advance for any help!
Update: I have completed this task using pivot tables and tediously changing the cells to make sure I have the correct December in my DJF seasonal average, but I would still like any guidance how to do this again in the future.
May not be any better than the query you have (who knows?)
In query union to ignore DEC in it's year and union to a query which pushes the DEC year forward
DROP TABLE IF EXISTS T;
create table t
(dt date, val int);
insert into t values
('2019-12-01',10),
('2020-01-01',25),
('2020-06-01',10),
('2020-10-01',10),
('2020-12-01',10);
select yyyy,
sum(djfsum) / sum(djfcnt) dfj ,
sum(mamcnt) / sum(mamcnt) mam ,
sum(jjasum) / sum(jjacnt) jja ,
sum(sonsum) / sum(soncnt) son
from
(
select year(t.dt) yyyy,
sum(case when MONTH(dt) IN(1,2) then val else 0 end) djfsum,
sum(case when MONTH(dt) IN(1,2) then 1 else 0 end) djfcnt,
sum(case when MONTH(dt) IN(3,4,5) then val else 0 end) mamsum,
sum(case when MONTH(dt) IN(3,4,5) then 1 else 0 end) mamcnt,
sum(case when MONTH(dt) IN(6,7,8) then val else 0 end) jjasum,
sum(case when MONTH(dt) IN(6,7,8) then 1 else 0 end) jjacnt,
sum(case when MONTH(dt) IN(9,10,11) then val else 0 end) sonsum,
sum(case when MONTH(dt) IN(9,10,11) then 1 else 0 end) soncnt
from t
where month(dt) <> 12
group by yyyy
union
select year(dt) + 1,
sum(val) as djfsum,
sum(1) as djfcnt,
0,
0,
0,
0,
0,
0
from t
where month(dt) = 12
group by year(dt) + 1
) a
group by yyyy
;
+------+---------+------+---------+---------+
| yyyy | dfj | mam | jja | son |
+------+---------+------+---------+---------+
| 2020 | 17.5000 | NULL | 10.0000 | 10.0000 |
| 2021 | 10.0000 | NULL | NULL | NULL |
+------+---------+------+---------+---------+
2 rows in set, 4 warnings (0.002 sec)
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
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;
I am trying to make an awesome custom table which counts the amount of rows and organises them so that if there are three rows with a date in January 2013, four in March 2014 and five in October 2014 the table would show up as:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2013 3 0 0 0 0 0 0 0 0 0 0 0
2014 0 0 4 0 0 0 0 0 0 5 0 0
I would recommend using a view, rather than a new table, this way when your underlying data changes your new table won't be out of sync.
Since you have not given much sample data I have had to assume a structure, but you would want something like this:
CREATE TABLE T (`Date` DATETIME);
INSERT T (`Date`)
VALUES
('2013-01-01'), ('2013-01-02'), ('2013-01-03'),
('2014-03-01'), ('2014-03-02'), ('2014-03-03'),
('2014-10-01'), ('2014-10-01'), ('2014-10-01'),
('2014-10-01'), ('2014-10-01');
CREATE VIEW V
AS
SELECT YEAR(`Date`) AS `Year`,
COUNT(CASE WHEN MONTH(`Date`) = 1 THEN 1 END) AS `Jan`,
COUNT(CASE WHEN MONTH(`Date`) = 2 THEN 1 END) AS `Feb`,
COUNT(CASE WHEN MONTH(`Date`) = 3 THEN 1 END) AS `Mar`,
COUNT(CASE WHEN MONTH(`Date`) = 4 THEN 1 END) AS `Apr`,
COUNT(CASE WHEN MONTH(`Date`) = 5 THEN 1 END) AS `May`,
COUNT(CASE WHEN MONTH(`Date`) = 6 THEN 1 END) AS `Jun`,
COUNT(CASE WHEN MONTH(`Date`) = 7 THEN 1 END) AS `Jul`,
COUNT(CASE WHEN MONTH(`Date`) = 8 THEN 1 END) AS `Aug`,
COUNT(CASE WHEN MONTH(`Date`) = 9 THEN 1 END) AS `Sep`,
COUNT(CASE WHEN MONTH(`Date`) = 10 THEN 1 END) AS `Oct`,
COUNT(CASE WHEN MONTH(`Date`) = 11 THEN 1 END) AS `Nov`,
COUNT(CASE WHEN MONTH(`Date`) = 12 THEN 1 END) AS `Dec`
FROM T
GROUP BY YEAR(`Date`);
Example on SQL Fiddle