我在使用以下代码时遇到了一些问题。我正在研究一个顶级篮子问题,我试图计算篮子组合的数量,其中每个交易应该只有4个项目。以下代码运行良好,但当事务购买额外的4个项目(或更多)时,它将不会计入combined_basket步骤中。
With RowNums as (
select
edw_transaction_id, row_number() over (partition by edw_transaction_id order by article_name) as row_id, article_name
from thing1
),
BasketItems as (
select a.edw_transaction_id, a.article_name as _1, b.article_name as_2,c.article_name as _3,d.article_name as _4
from (select edw_transaction_id,article_name from RowNums where row_id =1) a
join (select edw_transaction_id,article_name from RowNums where row_id =2) b
on a.edw_transaction_id = b.edw_transaction_id
join (select edw_transaction_id,article_name from RowNums where row_id =3) c
on a.edw_transaction_id = c.edw_transaction_id
join (select edw_transaction_id,article_name from RowNums where row_id =4) d
on a.edw_transaction_id = d.edw_transaction_id
),
combined_basket as (
select count(*) as basket_count, _1 as basket_item1,_2 as basket_item2,_3 as basket_item3,_4 as basket_item4
from BasketItems
group by 2,3,4,5
order by 1 desc
)
select *
from combined_basket
order by 1 desc
limit 10
BasketItems的输出看起来像这样:
Trans Id Row_num Article_Name
6368773827 1 Apples
6368773827 2 Oranges
6368773827 3 Cheese
6368773827 4 Egg
6368774403 1 Apple
6368774403 2 Egg
6368774403 3 Cake
6368774403 4 Salad
6368774403 5 Egg
6368774403 6 Apple
6368774403 7 Lemon
6368774403 8 Burger
正如您从上面的代码中可以看出的那样,它只会计算前4个项目并保留其余项目(对于每个事务)。有没有办法在超过4时重复row_number函数或是否有另一种解决此问题的方法,因此计算4的每个事务?
所需的结果将是交易,每个项目标记为1到4。