2013-06-06 91 views
0

我有一個表與銷售以下數據SQL查詢來獲取最高售價爲每

 
Item_ID Rate Quantity Sale_Day (YYYY-MM-DD) 
-------------------------------------- 
P1  4  4  2013-06-02 
P1  5  2  2013-06-02 
P2  1  8  2013-06-02 
P1  6  3  2013-06-03 
P2  4  9  2013-06-03 
P3  10  10  2013-06-03 

我需要的結果如下:

 
Item_ID Total Sale_Day (YYYY-MM-DD) 
-------------------------------------- 
P1  26  2013-06-02 
P3  100  2013-06-03 

任何幫助將不勝感激。謝謝!

回答

0

嘗試

SELECT Sale_Day, Item_ID, Total 
FROM 
(
SELECT Sale_Day, Item_ID, 
     SUM(Rate*Quantity) Total, 
     RANK() OVER(PARTITION BY Sale_Day ORDER BY SUM(Rate*Quantity) DESC) rank 
    FROM sale 
GROUP BY Sale_Day, Item_ID 
) a 
WHERE rank = 1 
ORDER BY Sale_Day 

輸出:

|     SALE_DAY | ITEM_ID | TOTAL | 
------------------------------------------------- 
| June, 02 2013 00:00:00+0000 |  P1 | 26 | 
| June, 03 2013 00:00:00+0000 |  P3 | 100 | 

這裏是SQLFiddle演示

+0

真棒!非常感謝。 :) – user2389390