1

我想返回特定 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        -||-

我怎么得到这个日期?

4

2 回答 2

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])

这可能不是最有效的方法,但它应该可以工作......

于 2013-09-14T16:13:25.903 回答
0

计算 ID 集及其最大值,然后将其与原始集相交。

SELECT * FROM t
JOIN (SELECT id, MAX(value) AS value FROM t GROUP BY id) AS max_t
USING (id, value)
于 2013-09-15T05:44:50.863 回答