为糟糕的头衔道歉 - 无法想出更好的东西。
我有下表:
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
问题:
答案 0 :(得分:0)
尝试row_number函数。您必须构建2个派生表(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
(从(选择等等等等)选择等等等等等等物品
--now inner join your 2 derived tables
where custs.Customer_ID, = items.Customer_ID