2016-08-11 80 views
0

我們有一個包含三列的ORDERS表:CUSTOMER_ID,ORDER_ID和PRODUCTS_ID。示例數據:先前總數的SQL百分比

CUSTOMER_ID ORDER_ID PRODUCT_ID ORDER_DAY 
C1   O1   P1   1-Jan-15 
C1   O1   P2   1-Jan-15 
C1   O1   P3   1-Jan-15 

C2   O2   P6   2-Jan-15 
C2   O2   P1   2-Jan-15 
C2   O2   P3   2-Jan-15 

C1   O3   P1   3-Jan-15 
C1   O3   P3   3-Jan-15 
C1   O3   P6   3-Jan-15 
C1   O3   P7   3-Jan-15 

您可以編寫一個查詢以獲得以下輸出嗎?

ORDER_ID #PRODUCTS #PRODUCTS_IN_PAST %PRODUCTS_IN_PAST 
O1   3   0     0% 
O2   3   0     0% 
O3   4   2     50% 

其中:

#PRODUCTS: total number of products purchased in the order 
    #PRODUCTS_IN_PAST is the number of the products in the particular order that were purchased by the same customer in the past. 
    %PRODUCTS_IN_PAST = #PRODUCTS_IN_PAST/#PRODUCTS 
+2

請與您正在使用的數據庫標記您的問題。 –

+1

你到目前爲止嘗試過什麼?這是可行的 - 在某些DBMS中比其他的更容易 - 但是,您應該顯示您的努力。 – Nicarus

+0

我是一個SQL新手,所以我被這個問題困住了。另外,我一直在SAS和Python中使用一些SQL過程,但數據庫本身並不重要。我會蔑視MySQL。 – James

回答

0
select order_id, count(*) as numproducts, sum(is_previous), avg(is_previous) 
from (select o.*, 
      (case when o.product_id in (select o2.product_id 
             from orders o2 
             where o2.customer_id = o.customer_id and 
               o2.order_date < o.order_date 
             ) 
        then 1 else 0 
       end) as is_previous 
     from orders o 
    ) o 
group by o.order_id;