SQL 在事实 table 中过滤计数使用子查询或更好的替代方法

SQL filtering counts in a fact table using a subquery or better alternative

我正在尝试将我的数据仓库中的数据汇总成一个事实 table 并且我希望能够计算每个客户每个月的每种类型的交易数量。

正在使用的列是:Customer_id、Transaction_id、transaction_date、Transaction_type

理想情况下我想要得到的是

Customer Month transaction_type_1 transaction_type_2 Total_transactions
12345 1 18 8 26
12345 2 23 14 37
67891 1 14 22 36

我必须将它放入子查询中,但我得到了每个月所有客户的类型 1 交易总数。我曾尝试在此之上使用分区但未成功,但现在远远超出了我的水平。

Select 
 customer_id, 
 month, 
 count(transactions_id),
 (select count(transactions_id) from DWH where transaction_type = 1),
 (select count(transactions_id) from DWH where transaction_type = 2)
FROM DWH
GROUP BY customer_id, month

不正确的 table 输出看起来像这样。

Customer Month transaction_type_1 transaction_type_2 Total_transactions
12345 1 432 564 26
12345 2 456 765 37

在独立的 table 中,我可以获得信息,但无法将其合并到事实 table 视图中。

独立的它可以获取每种类型的单独计数,但我无法将其重新处理为 select 子查询:

select customer_id, month, count(*) 
   FROM DWH 
   WHERE dwh.transaction_type = 1
   Group BY dwh.customer_id, month;

如有任何帮助,我们将不胜感激。

您可能得到不正确的结果,因为您各自子查询的 where 子句中的过滤器不考虑按列分组,即

table 别名 d 帮助我们区分 outer/general 查询和子查询中使用的列。

select 
 d.customer_id, 
 d.month, 
 count(d.transactions_id),
 (
     select count(transactions_id) 
     from DWH 
     where transaction_type = 1 and 
           customer_id = d.customer_id and
           month = d.month
 ) as transaction_type_1,
 (
     select count(transactions_id) 
     from DWH 
     where transaction_type = 2 and 
           customer_id = d.customer_id and
           month = d.month
 ) as transaction_type_2
FROM DWH d
GROUP BY d.customer_id, d.month

然而,虽然这种方法可能有效,但最好是您在各自的数据库上明智地测试了这种性能并评估成本 metrics/query 计划。

另一种可能更高效的方法使用 case 表达式来实现结果,并已包含在下面:

SELECT
     customer_id as Customer,
     month,
     COUNT(
         CASE WHEN transaction_type=1 THEN transactions_id END
     ) as transaction_type_1,
     COUNT(
         CASE WHEN transaction_type=2 THEN transactions_id END
     ) as transaction_type_2,
     COUNT(1) as Total_transactions
FROM
    DWH
GROUP BY
    customer_id, month