我在查詢的問題:SQL查詢的問題 -
priceDeatil
ProductCode BusinessUnit price DateFrom DateTo
10001 ORB 12.00 12-08-2011 31-09-2015
10001 ORB 21.00 01.08-2011 15-11-2011
10002 ORB 31.00 01.04-2011 15-08-2012
10003 ORB 42.00 01.05-2011 15-08-2012
我的查詢是:
SELECT BusinessUnit, ProductCode, DateFrom, DateTo, Price
FROM WMPriceDetail
WHERE (DateFrom < 'Sep 01 2011') AND (DateTo > 'Sep 01 2011' OR DateTo = '')
ORDER BY ProductCode
這回
priceDeatil
ProductCode BusinessUnit price DateFrom DateTo
10001 ORB 12.00 12-08-2011 31-09-2015
10001 ORB 21.00 01.08-2011 15-11-2011
10002 ORB 31.00 01.04-2011 15-08-2012
10003 ORB 42.00 01.05-2011 15-08-2012
但這裏productCode 10001
返回兩個記錄,那一次我想要獲得最大日期即12-08-2011
。
所以理想的結果應該是這樣的:
priceDeatil
ProductCode BusinessUnit price DateFrom DateTo
10001 ORB 12.00 12-08-2011 31-09-2015
10002 ORB 31.00 01.04-2011 15-08-2012
10003 ORB 42.00 01.05-2011 15-08-2012
請幫我...如何編寫查詢這種情況呢?
在此先感謝
什麼版本的SQL Server?爲什麼你把它標記爲mysqli? –
只是希望這不是真正的數據,因爲2015年9月只有30天:) – Kyro