2016-12-08 25 views
0

對於蹩腳的標題抱歉 - 無法提供更好的東西。SQL排序 - 選擇頂行內的頂部行

我有如下表:

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 

問題:

  • 我基本上是加入表本身在這裏,有沒有更好的辦法?
  • 如何限制查詢僅返回每個Customer_ID的前100個項目?此處的外部限制將限制所有行,而不是每個客戶的第一個X.

回答

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 
+0

這裏的排名只會讓你基礎上,Sale_TScolumn無關銷售數量的行數。 – Matt