1
對此有點困難..我試圖根據區域和產品獲取最新記錄。SQL最近行
我已經試過:
SELECT T.Region,
T.Product,
T.Date,
T.Revenue,
T.Cost,
FROM Table as T
Inner Join (
Select Region,
Product,
Max(Date) as Date
FROM Table
Group By Region, Product) as X
On T.Product = X.Product
And T.Region = X.Region
And T.Date = X.Date
表:
Region Product Date Revenue Cost
A X 10/1/2015 21967 0
A X 1/31/2016 19935 19935
A Z 1/1/1987 20356 0
A Z 10/1/1994 20356 0
B X 1/31/2016 19942 19942
B X 2/1/2016 21238 0
B Z 1/1/2004 19942 0
B Z 10/1/2014 19942 0
C Z 1/1/1987 20354 0
C Z 4/1/2002 21972 0
C Z 1/31/2016 21972 21972
期望的結果:
Region Product Date Revenue Cost
A X 1/31/2016 19935 19935
A Z 10/1/1994 20356 0
B X 2/1/2016 21238 0
B Z 10/1/2014 19942 0
C Z 1/31/2016 21972 21972
您面臨的問題是什麼?你使用的是哪個數據庫? –
您當前的查詢有什麼問題? – jarlh