sql - 如何在 SQL 中聚合数量?
问题描述
我正在尝试查找拆分和单笔交易的数量总和、txns 计数、dollar_value_us 总和以及保证金的计算。
下面是我创建的数据库的链接:
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=97be115a173cd7dbdb3e4a6a6ed35a16
我已经有这个查询来查找单个和拆分的事务数:
select count(distinct case when is_split = 'Yes' then transaction_number end) as split,
count(distinct case when is_split = 'No' then transaction_number end) as single
from (
select td.transaction_number, td.sku,
case when count(ps.sku) over (partition by td.transaction_number) > 0
then 'Yes'
else 'No'
end as has_pod_sku,
case when count(ps.sku) over (partition by td.transaction_number) > 0
and count(ps.sku) over (partition by td.transaction_number)
< count(*) over (partition by td.transaction_number)
then 'Yes'
else 'No'
end as is_split
from transaction_detail_mv td
left join pod_sku ps on ps.sku = td.sku
)
where has_pod_sku = 'Yes';
我正在使用以下代码进行聚合:
select count(distinct case when is_split = 'Yes' then transaction_number end) as split,
count(distinct case when is_split = 'No' then transaction_number end) as single,
sum(case when is_split = 'Yes' then quantity end) as quantity_sp,
sum(case when is_split = 'No' then quantity end) as quantity_sn,
sum(case when is_split = 'Yes' then dollar_value_us end) as spend_sp,
sum(case when is_split = 'No' then dollar_value_us end) as spend_sn,
count(distinct case when is_split = 'Yes' then individual_id end) as indiv_sp,
count(distinct case when is_split = 'No' then individual_id end) as indiv_sn,
sum(case when is_split = 'Yes' then (DOLLAR_VALUE_US-(COGS*quantity)) end) as MARGIN_sp,
sum(case when is_split = 'No' then (DOLLAR_VALUE_US-(COGS*quantity)) end) as MARGIN_sN
from (
select td.transaction_number, td.sku,
case when count(ps.sku) over (partition by td.transaction_number) > 0
then 'Yes'
else 'No'
end as has_pod_sku,
case when count(ps.sku) over (partition by td.transaction_number) > 0
and count(ps.sku) over (partition by td.transaction_number)
< count(*) over (partition by td.transaction_number)
then 'Yes'
else 'No'
end as is_split
from transaction_detail_mv td
left join pod_sku ps on ps.sku = td.sku
)
where has_pod_sku = 'Yes';
我正在寻找如下输出:
解决方案
而不是在列中聚合每个事务类型(拆分/单一),GROUP BY
事务类型:
SELECT v.transaction_type
, COUNT(DISTINCT v.transaction_number) AS transaction_count
, COUNT(DISTINCT v.individual_id) AS customer_count
, SUM(v.quantity) AS units
, SUM(v.dollar_value_us) AS sales
, SUM(v.dollar_value_us - v.cogs * v.quantity) AS profit
FROM (SELECT td.transaction_number, td.sku, td.quantity, td.dollar_value_us, td.individual_id, td.cogs,
CASE
WHEN COUNT(ps.sku) OVER (PARTITION BY td.transaction_number) > 0
THEN 'Yes'
ELSE 'No'
END AS has_pod_sku,
CASE
WHEN COUNT(ps.sku) OVER (PARTITION BY td.transaction_number) > 0
AND COUNT(ps.sku) OVER (PARTITION BY td.transaction_number)
< COUNT(*) OVER (PARTITION BY td.transaction_number)
THEN 'Split'
ELSE 'Single'
END AS transaction_type
FROM transaction_detail_mv td
LEFT OUTER
JOIN pod_sku ps on ps.sku = td.sku
) v
WHERE has_pod_sku = 'Yes'
GROUP BY v.transaction_type
工作查询。
推荐阅读
- numpy - 删除图像 OpenCV 的背景
- javascript - JavaScript 切换类
- firebase - 如何在 Flutter 上获取 Firebase Firestore 异常代码?
- php - Wordpress 查询关系 AND 与 meta_query 和 tax_query
- java - 是否可以在 pycharm ide 中使用 java 模块编译和执行项目?
- python - 使用 for 循环查找偶数或奇数的代码问题
- flutter - Flutter类报错,尝试添加初始化表达式,或者在这个构造函数中添加字段初始化,或者标记一下
- python - AttributeError:“spacy.tokens.span.Span”对象没有属性“字符串”
- python - 根据定义的列数自动创建熊猫列
- flask - 连接到 5000 端口后如何输入命令?