简体   繁体   English

SQL如何按日期和多个条件聚合

[英]SQL how to aggregate by date and multiple criteria

I have this table:我有这张桌子:

id ID ammount数量 method方法 date日期
01 01 10 10 A一个 2022-01-24 12:27:14.440 2022-01-24 12:27:14.440
02 02 80 80 A一个 2022-01-24 12:27:14.440 2022-01-24 12:27:14.440
01 01 20 20 D D 2022-02-24 12:27:14.440 2022-02-24 12:27:14.440
01 01 10 10 D D 2022-02-24 12:27:14.440 2022-02-24 12:27:14.440
02 02 20 20 D D 2022-02-24 12:27:14.440 2022-02-24 12:27:14.440
03 03 30 30 D D 2022-02-24 12:27:14.440 2022-02-24 12:27:14.440

and I want this:我想要这个:

method方法 ammount_sum_jan ammount_sum_jan n_transaction_jan n_transaction_jan n_customer_jan n_customer_jan ammount_sum_feb ammount_sum_feb n_transaction_feb n_transaction_feb n_customer_feb n_customer_feb
A一个 10 10 2 2 2 2 0 0 0 0 0 0
D D 0 0 0 0 0 0 80 80 4 4 3 3

This is a table with 7 column and rows equal to the number of methods.这是一个包含 7 列和行数的表,等于方法的数量。

  • AMMOUNT: sum of ammount in one month of one method AMMONT:一种方法一个月内的金额总和
  • N_TRANSACTIONS: number of transaction in one month with one method N_TRANSACTIONS:一种方法一个月内的交易数量
  • N_CUSTOMER: number of customers (id) who used that method in one month N_CUSTOMER:一个月内使用该方法的客户(id)数

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.您希望按方法汇总数据,并为 1 月数据和 2 月数据设置单独的列。 You get this with conditional aggregation ( CASE expression inside the aggregate function),您可以通过条件聚合(聚合函数中的CASE表达式)得到它,

select
  method,
  sum(case when month(date) = 1 then ammount else 0 end) as ammount_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 ammount else 0 end) as ammount_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 dfix dates look like this.它被称为枢轴,并且对于 dfix 日期看起来像这样。 An aggregation of the methid 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 `ammount` ELSe 0 END) ammount_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 `ammount` ELSe 0 END) ammount_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 http://www.sqlfiddle.com/#!9/31d8ef/10

much more interesting would be to ake that dynamic更有趣的是让这种动态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM