嘿,我有點卡在這個查詢。使用SQL服務器根據聚合獲得最新的日期行
我有表,UNIQUE(日期,medId,用戶id)
我有這個表
date | medId | userId | Quantity
2016-06-10 | 2 | 1 | 28
2016-06-07 | 1 | 1 | 19
2016-06-06 | 1 | 1 | 10
我想用最大日行,每組medId,用戶id的,在這種情況下 我會得到
2016-06-10 | 2 | 1 | 28
2016-06-07 | 1 | 1 | 19
在此先感謝!
我已經試過這
SELECT
a.userMedStockDate,
a.userMedStockMedId,
a.userMedStockUserId,
a.userMedStockQuantity
FROM (SELECT
MAX(userMedStockDate) AS userMedStockDate,
userMedStockQuantity,
userMedStockUserId,
userMedStockMedId,
ROW_NUMBER() OVER (partition by userMedStockMedId,userMedStockUserId
ORDER BY MAX(userMedStockDate) desc) AS rnk
FROM UserMedStock
GROUP BY
userMedStockUserId,
userMedStockQuantity,
userMedStockMedId) a
WHERE a.rnk = 1
[解決]
不起作用,但謝謝 – franmcod