sql行数()repeat

ttisahbt  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(376)

下面的代码有点问题。我正在处理一个顶部篮子问题,我试图计算篮子组合的数量,其中每个交易应该只有4个项目。下面的代码工作得很好,但是当一个事务购买了额外的4个项目(或更多)时,它将不会被计算在合并的\u篮子步骤中。

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时,是否有方法重复行数函数?或者是否有其他方法来解决此问题,以便对4的每个事务进行计数?
期望的结果是每个事务项都标记为1到4。

falq053o

falq053o1#

您可以添加一个数学函数,即行号%4。这将解决问题,即使没有分区:
行号()在(按项目名称排序)%4上作为行id
尽管如此,我不能100%确定mysql中是否存在%作为符号。如果没有,请查找模块化除法。

相关问题