我在一个单独的桌子上有一张SKU的清单,里面只有POD。我需要找到包含POD SKU的事务,还有POD SKU和其他SKU的事务。
我已经附上了一个我创建的样本数据库。
11.2&fiddle=c2150662b298b8d172c6902514ada52a
这是这些表格中的数据:
select * from transaction_detail_mv;
INDIVIDUAL_ID DOLLAR_VALUE_US QUANTITY TRANSACTION_NUMBER SKU TXN_DATE BRAND IS_MERCH CURRE L
------------- --------------- ---------- ------------------ ---------- ---------- ----- ---------- ----- -
1 10 30 567 903633 2019-02-01 BRAND 1 USD S
1 20 30 567 123767 2019-02-01 BRAND 1 USD S
1 10 40 345 773633 2019-02-10 BRAND 1 USD S
1 12 30 345 965322 2019-02-10 BRAND 1 USD S
1 10 50 678 838364 2019-02-15 BRAND 1 USD S
1 10 70 975 983636 2019-02-28 BRAND 1 USD S
2 11 80 910 363635 2019-02-11 BRAND 1 USD S
2 11 90 323 566373 2019-02-12 BRAND 1 USD S
3 11 62 855 678364 2019-02-12 BRAND 1 USD Sselect * from POD_SKU;
SKU
----------
903633
773633
838364
678364例如,事务号567和345将是拆分事务,因为它们在同一事务中各有一个POD SKU和一些其他SKU。事务975和855将是单个事务,因为它们只有POD SKU。
发布于 2019-03-29 17:30:58
您可以在表之间使用外部连接来同时包含POD和非POD SKU,然后使用case表达式和可能的分析(加窗)函数来比较同一事务的跨行;例如:
select td.transaction_number, td.sku,
case when ps.sku is not null
then 'Yes'
else 'No'
end as is_pod_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;
TRANSACTION_NUMBER SKU IS_POD_SKU HAS_POD_SKU IS_SPLIT
------------------ ---------- ---------- ----------- --------
323 566373 No No No
345 773633 Yes Yes Yes
345 965322 No Yes Yes
567 123767 No Yes Yes
567 903633 Yes Yes Yes
678 838364 Yes Yes No
855 678364 Yes Yes No
910 363635 No No No
975 983636 No No No 然后,您可以过滤掉has_pod_sku为“否”的那些。显然(希望)您可以从原始表中包含任何您想要的列,我刚刚向您展示了两个在这一点上似乎最相关的列。如果“是”和“否”,则可以使用0/1或Y/N标志或更多的描述性文本。细节取决于你到底想要看到什么。
有没有办法数数?
您可以使用上面的查询作为子查询并计数每个标志值(不包括那些根本没有POD SKU的):
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';
SPLIT SINGLE
---------- ----------
2 2或者更简单地说:
select count(case when sku_count > pod_sku_count then transaction_number end) as split,
count(case when sku_count = pod_sku_count then transaction_number end) as single
from (
select td.transaction_number,
count(*) as sku_count,
count(ps.sku) as pod_sku_count
from transaction_detail_mv td
left join pod_sku ps on ps.sku = td.sku
group by td.transaction_number
)
where pod_sku_count > 0;
SPLIT SINGLE
---------- ----------
2 2添加了这两个查询的db<>fiddle。
https://stackoverflow.com/questions/55422324
复制相似问题