SQL how to aggregate by date and multiple criteria - mysql

I have this table:
id
amount
method
date
01
10
A
2022-01-24 12:27:14.440
02
80
A
2022-01-24 12:27:14.440
01
20
D
2022-02-24 12:27:14.440
01
10
D
2022-02-24 12:27:14.440
02
20
D
2022-02-24 12:27:14.440
03
30
D
2022-02-24 12:27:14.440
and I want this:
method
amount_sum_jan
n_transaction_jan
n_customer_jan
amount_sum_feb
n_transaction_feb
n_customer_feb
A
10
2
2
0
0
0
D
0
0
0
80
4
3
This is a table with 7 column and rows equal to the number of methods.
AMOUNT: sum of amount in one month of one method
N_TRANSACTIONS: number of transaction in one month with one method
N_CUSTOMER: number of customers (id) who used that method in one month
Can I get it with just one query?

You want to aggregate your data by method and have separate columns for January data and February data. You get this with conditional aggregation (CASE expression inside the aggregate function),
select
method,
sum(case when month(date) = 1 then amount else 0 end) as amount_sum_jan,
count(case when month(date) = 1 then 1 end) as n_transaction_jan,
count(distinct case when month(date) = 1 then id end) as n_customer_jan,
sum(case when month(date) = 2 then amount else 0 end) as amount_sum_feb,
count(case when month(date) = 2 then 1 end) as n_transaction_feb,
count(distinct case when month(date) = 2 then id end) as n_customer_feb
from mytable
group by method
order by method;

It is called pivot, and would for fix dates look like this.
An aggregation of the method for the year and month, and you can COUNT or SUM your number
select
`method`,
SUM(CASE WHEN EXTRACT( YEAR_MONTH FROM `date` ) = '202201' then `amount` ELSE 0 END) amount_sum_jan,
SUM(CASE WHEN EXTRACT( YEAR_MONTH FROM `date` ) = '202201' then 1 ELSE 0 END) n_transaction_jan,
COUNT(DISTINCT CASE WHEN EXTRACT( YEAR_MONTH FROM `date` ) = '202201' then `d` ELSE 0 END) n_customer_jan,
SUM(CASE WHEN EXTRACT( YEAR_MONTH FROM `date` ) = '202202' then `amount` ELSe 0 END) amount_sum_feb,
SUM(CASE WHEN EXTRACT( YEAR_MONTH FROM `date` ) = '202202' then 1 ELSe 0 END) n_transaction_feb,
COUNT(DISTINCT CASE WHEN EXTRACT( YEAR_MONTH FROM `date` ) = '202202' then `d` ELSe 0 END) n_customer_feb
from tab1
GROUP BY `method`
http://www.sqlfiddle.com/#!9/31d8ef/10
much more interesting would be to make that dynamic

Related

Query with multiple AND conditions

I have my query like below
SELECT dates,
COUNT(link_data_id) as TotalClicks,
sum(case when link_redirect_status = 1 then 1 else 0 end) AS GoodClicks,
sum(case when link_redirect_status != 1 then 1 else 0 end) AS BadClicks
FROM tbl_calendar
LEFT JOIN tbl_links_data ON dates = CAST(link_data_time AS DATE)
WHERE (`dates` BETWEEN '2021-11-28' AND DATE_ADD('2021-11-28', INTERVAL 7 DAY))
GROUP BY dates
It's giving me expected output like below
But I want add one more condition called link_order_id ='abcde', so I am trying like below
SELECT dates,
COUNT(link_data_id) as TotalClicks,
sum(case when link_redirect_status = 1 then 1 else 0 end) AS GoodClicks,
sum(case when link_redirect_status != 1 then 1 else 0 end) AS BadClicks
FROM tbl_calendar
LEFT JOIN tbl_links_data ON dates = CAST(link_data_time AS DATE)
WHERE link_order_id = 'abcde'
AND (`dates` BETWEEN '2021-11-28' AND DATE_ADD('2021-11-28', INTERVAL 7 DAY))
GROUP BY dates
But it's giving me only two rows like below
Why I am getting only two rows instead of 8 rows like first picture?
Move the criteria in the WHERE clause to the ON clause of the join:
SELECT
c.dates,
COUNT(d.link_data_id) AS TotalClicks,
SUM(CASE WHEN d.link_redirect_status = 1 THEN 1 ELSE 0 END) AS GoodClicks,
SUM(CASE WHEN d.link_redirect_status != 1 THEN 1 ELSE 0 END) AS BadClicks
FROM tbl_calendar c
LEFT JOIN tbl_links_data d
ON c.dates = CAST(d.link_data_time AS DATE) AND
d.link_order_id = 'abcde'
WHERE (c.dates BETWEEN '2021-11-28' AND DATE_ADD('2021-11-28', INTERVAL 7 DAY))
GROUP BY c.dates;

I am trying to produce seasonal averages of visibility using already calculated monthly averages, but struggling

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)

MySQL - How to populate weekly data in a month

How to select data from following table group by weeks in a month
Date Project Value Week
+----------+--------------+-------+------+
2018-11-07 A 2 45
2018-11-08 B 4 45
2018-11-09 C 3 45
2018-11-12 B 6 46
2018-11-13 A 5 46
2018-11-14 C 6 46
(First week is end on sunday or week number in a month)
So my result should look like this.
Project 1st Week 2nd Week 3rd Week 4th Week 5th Week
+----------+--------+--------+--------+--------+--------
A 0 2 5 0 0
B 0 4 6 0 0
C 0 3 6 0 0
I try this one :
SELECT project, value, week, date
FROM module_progress
WHERE
created_at BETWEEN '2018-11-01 00:00:00' AND '2018-11-31
AND date > DATE_SUB(NOW(), INTERVAL 1 WEEK) 23:59:59'
GROUP BY week
Thank you
Just use a sub query to get first week no for the month, and sum case statements for each week:
select year(date) as y, month(date) as m, project,
sum(case when week=w0 then value else 0 end) as w1,
sum(case when week=w0+1 then value else 0 end) as w2,
sum(case when week=w0+2 then value else 0 end) as w3,
sum(case when week=w0+3 then value else 0 end) as w4,
sum(case when week=w0+4 then value else 0 end) as w5
from #date d
join (select year(date) as y, month(date) as m, min(week) as w0 from #date group by year(date), month(date))
as d0 on d0.y=year(date) and d0.m=month(date)
group by year(date), month(date), project

How do I count records based on time (month, year) ?

I have a table (myItems) with an item "id" and "date". Now i want to read out, how many items there are per month (i also want to distinguish between October 2013 and October 2014).
I started with:
SELECT Count(okt.id) AS Oktober, Count(nov.id) AS November
FROM `myItems` as okt,
`myItems` as nov
WHERE (okt.date between '2013-10-01' and '2013-10-31')
OR (nov.date between '2013-11-01' and '2013-11-30')
But it prints out a ridiculously large number. What am i doing wrong?
Try this. This will divide data into months and then do the COUNT :
SELECT SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) AS Oktober
, SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) AS November
FROM `myItems`
Demo: SQL Fiddle
With YEAR integrated:
SELECT 2013 as Year
, SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) AS Oktober
, SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) AS November
FROM `myItems`
WHERE YEAR(date) = 2013
UNION ALL
SELECT 2014 as Year
, SUM(CASE WHEN MONTH(date) = 10 THEN 1 ELSE 0 END) AS Oktober
, SUM(CASE WHEN MONTH(date) = 11 THEN 1 ELSE 0 END) AS November
FROM `myItems`
WHERE YEAR(date) = 2014;
;
Demo: Fiddle
With inspiration from #user77318
SELECT YEAR(date) as Year, month(date) as month, count(id) as count
FROM myItems
GROUP BY YEAR(date), MONTH(date);
I personally recommend this, more beautiful. Then you can do all the presentation stuffs on Application Layer.
Try this. Group the result by month:
SELECT month(date) as month, count(id) as count FROM myItems WHERE date between '2013-10-01' and '2013-11-30' GROUP BY MONTH(date);
Example of output result:
Month | Count
10 | 100
11 | 200
Probably you are not using join, you are just giving the conditions. First try to join these 2 tables with the primary keys or with some unique and common data column. Then try to execute above query. Somthiong like this:-
SELECT Count(okt.id) AS Oktober, Count(nov.id) AS November
FROM `myItems` as okt,
`myItems` as nov
WHERE okt.id = nov.id
AND (okt.date between '2013-10-01' and '2013-10-31')
OR (nov.date between '2013-11-01' and '2013-11-30');
May be this is helpful for you.

SQL adding Months

Hello I am trying to get records of certain months. For example for this query I would want the user to see the current month which is "May" and one month from the past which would be "April". I don't want my query to run any other month from that point on. I am stuck with this query and cant figure it out. Basically I need a function in my query to automatically know this month and the 1 month before to show the records. Thank you
DECLARE #Year int
set #Year = 2014
SELECT d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone
, COUNT(CASE WHEN MONTH(c.Funded_date) = 1 THEN 1 ELSE NULL END) January
, COUNT(CASE WHEN MONTH(c.Funded_date) = 2 THEN 1 ELSE NULL END) Feburary
, COUNT(CASE WHEN MONTH(c.Funded_date) = 3 THEN 1 ELSE NULL END) March
, COUNT(CASE WHEN MONTH(c.Funded_date) = 4 THEN 1 ELSE NULL END) April
, COUNT(CASE WHEN MONTH(c.Funded_date) = 5 THEN 1 ELSE NULL END) May
, COUNT(CASE WHEN MONTH(c.Funded_date) = 6 THEN 1 ELSE NULL END) June
, COUNT(CASE WHEN MONTH(c.Funded_date) = 7 THEN 1 ELSE NULL END) July
, COUNT(CASE WHEN MONTH(c.Funded_date) = 8 THEN 1 ELSE NULL END) August
, COUNT(CASE WHEN MONTH(c.Funded_date) = 9 THEN 1 ELSE NULL END) September
, COUNT(CASE WHEN MONTH(c.Funded_date) = 10 THEN 1 ELSE NULL END) October
, COUNT(CASE WHEN MONTH(c.Funded_date) = 11 THEN 1 ELSE NULL END) November
, COUNT(CASE WHEN MONTH(c.Funded_date) = 12 THEN 1 ELSE NULL END) December
, COUNT(*) 'Year to Date'
FROM tdealer a
JOIN tContact b ON a.contact_id = b.contact_id
JOIN tContract c ON a.dealer_id = c.dealer_id
JOIN tCompany d ON c.company_id = d.company_id
where YEAR (c.Funded_date) = #Year
GROUP BY d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone
The trick here is to do two things:
Create an expression which converts any arbitrary DATETIME into the first day of the month in which that date occurred.
Use it appropriately in WHERE and GROUP BY clauses.
The expression is
DATE_FORMAT(whatever, '%Y-%m-01')
This takes, for example '2014-05-28 12:22:30', and turns it into '2014-05-01'. It's really handy to convert one valid DATE to another valid DATE, because then you can use all the good date arithmetic built into MySQL.
So, for example, if you want the first of the month before the present month you use this:
DATE_FORMAT(NOW(), '%Y-%m-01') - INTERVAL 1 MONTH
Here's the outline of your query:
SELECT DATE_FORMAT(c.Funded_date, '%Y-%m-01') AS month_beginning,
d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone,
COUNT(1) AS itemcount
FROM tdealer a
JOIN tContact b ON a.contact_id = b.contact_id
JOIN tContract c ON a.dealer_id = c.dealer_id
JOIN tCompany d ON c.company_id = d.company_id
where c.Funded_date >= DATE_FORMAT(NOW(), '%Y-%m-01') - INTERVAL 1 MONTH
AND c.Funded_date < DATE_FORMAT(NOW(), '%Y-%m-01') + INTERVAL 1 MONTH
GROUP BY DATE_FORMAT(c.Funded_date, '%Y-%m-01'),
d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone
ORDER BY d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone,
DATE_FORMAT(c.Funded_date, '%Y-%m-01')
See how you choose the two-month period? It starts with the first day of the month before the present month, and ends with the first day of next month, but without including that day.
How to do this sort of thing is written up at http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/