sql排序-选择顶行中的顶行

bvpmtnay  于 2021-06-28  发布在  Hive
关注(0)|答案(1)|浏览(310)

为这个烂标题道歉-没能想出更好的。
我有下表:

Customer_ID    Item_ID        sale_ID   sale_TS
103293         I-0394039    S-430943    20161101

我需要找到销量最大的前100名客户,并为他们中的每一位找到他们在给定时间内购买的前100项商品。到目前为止,我的情况是:

select vs.Customer_ID, vs.Item_ID, count(*) count2
from sales.sales_import si1
join
(
    select Customer_ID, count(*) s_count2 from sales.sales_import where
    sale_TS between '2016-01-01' and '2016-01-31' group by Customer_ID order by sale_TS desc limit 100
)
si2
on si1.Customer_ID = si2.Customer_ID
where
si1.sale_TS between '2016-01-01' and '2016-01-31'
group by  vs.Customer_ID, vs.Item_ID
order by vs.Customer_ID, count2 desc limit 100

问题:
我基本上是把table连在一起,有更好的方法吗?
如何限制查询只返回每个客户id的前100个项目?这里的外部限制将限制所有行,而不是每个customerid的第一个x

ou6hu8tu

ou6hu8tu1#

请尝试使用row\u number函数。您必须构建两个派生表(from子句中使用的子查询)。一个给顾客,一个给他们的东西。内部联接子查询,以便仅从第一个派生表中返回的客户处获取项。

select * from
--get your top 100 customers
  (select * from 
     (select Customer_ID, row_number() OVER (order by sale_TS) as rank
     from sales_import
     where sale_TS between '2016-01-01' and '2016-01-31'
     group by Customer_ID)
   where rank <= 100) custs
 --now build out a derived table that picks out the top 100 items they purchased using the same method

(从(select blah blah blah)项中选择blah blah blah blah blah)

--now inner join your 2 derived tables
where custs.Customer_ID, = items.Customer_ID

相关问题