我想返回特定ID的最大值日期。看看这张表。
ID Date Value
___ ____ _____
4545 9/17/12 5
4545 9/16/12 100
4545 9/15/12 20
2121 9/16/12 12
2121 9/15/12 132
2121 9/14/12 4
9999 9/16/12 45
9512 9/15/12 128
9512 9/14/12 323
2002 9/17/12 45
结果应该是:
ID Date Value
___ ____ _____
4545 9/16/12 100 date for ID(4545) for max value(100) is "9/16/12"
2121 9/15/12 132 date for ID(2121) for max value1(32) is "9/15/12"
9999 9/16/12 45 date for ID(9999) for max value(45) is "9/16/12"
9512 9/14/12 323 -||-
2002 9/17/12 45 -||-
我如何得到这个日期?
答案 0 :(得分:0)
假设您正在使用SQL,您应该尝试这样的事情(这是MS SQL语法):
SELECT [ID], [Date], [Value]
FROM [YourTable] AS [SRC]
WHERE NOT EXISTS (SELECT *
FROM [YourTable] AS [OTHER]
WHERE [OTHER].[ID] = [SRC].[ID] AND [OTHER].[Date] > [SRC].[Date])
这可能不是最有效的方法,但它应该有用......
答案 1 :(得分:0)
计算ID的集合及其最大值,然后将其与原始集合相交。
SELECT * FROM t
JOIN (SELECT id, MAX(value) AS value FROM t GROUP BY id) AS max_t
USING (id, value)