具有最大日期值的不同列

时间:2015-12-14 08:47:58

标签: sql sql-server tsql

我们说我有一张桌子:

LpOpenTradeId   LPSource    SymbolId    Volume  CreatedUser CreatedDate
1               2           1           10.00   2           2015-12-11 00:00:00.000
2               2           4           12.00   2           2015-12-11 00:00:00.000
3               2           1           10.00   2           2015-12-11 10:53:00.000
4               2           3           1.00    2           2015-12-11 18:03:14.676
5               2           5           1.00    2           2015-12-14 09:38:33.691
6               2           3           2.00    2           2015-12-14 09:39:30.305
7               2           4           13.00   2           2015-12-14 09:43:13.916
8               3           1           15.00   2           2015-12-11 10:53:00.000

我想选择具有最大CreatedDates的卷的不同LPSource和SymbolId列。我的意思是目标结果集是:

            LPSource    SymbolId    Volume              CreatedDate
            2           1           10.00               2015-12-11 10:53:00.000
            2           4           13.00               2015-12-14 09:43:13.916
            2           3           2.00                2015-12-14 09:39:30.305
            2           5           1.00                2015-12-14 09:38:33.691
            3           1           15.00               2015-12-11 10:53:00.000

如何表达自己在T-SQL中使用此结果集?

谢谢,

3 个答案:

答案 0 :(得分:3)

您可以使用auto player = Player::createPlayer(this); addChild(player);

ROW_NUMBER

如果SELECT LPSource, SymbolId, Volume, CreatedDate FROM ( SELECT LPSource, SymbolId, Volume, CreatedDate, ROW_NUMBER() OVER (PARTITION BY LPSource, SymbolId ORDER BY CreatedDate DESC) AS rn FROM mytable) AS t WHERE t.rn = 1 关联,即多个记录在同一CreatedDate分区内共享相同的最大CreatedDate值,则上述查询将随机选择一条记录。在这种情况下,您可以使用LPSource, SymbolId选择所有记录。

答案 1 :(得分:1)

如果没有其他具有相同NOT EXISTS / LPSource的行(1)后来SymbolId或(2)相同{{1},请使用CreatedDate返回一行但是更高CreatedDate

Volume

答案 2 :(得分:1)

IF OBJECT_ID('Tabel1','U') IS NOT NULL
BEGIN
    DROP TABLE Tabel1
END
CREATE TABLE Tabel1 (LpOpenTradeId INT
,LPSource INT   
,SymbolId INT    
,Volume DECIMAL(10,2)  
,CreatedUser INT
,CreatedDate DATETIME2
)
INSERT INTO Tabel1 VALUES (1,2,1,10,2,'2015-12-11 00:00:00.000');
INSERT INTO Tabel1 VALUES (2,2,4,12,2,'2015-12-11 00:00:00.000');
INSERT INTO Tabel1 VALUES (3,2,1,10,2,'2015-12-11 10:53:00.000');
INSERT INTO Tabel1 VALUES (4,2,3,1,2,'2015-12-11 18:03:14.676');
INSERT INTO Tabel1 VALUES (5,2,5,1,2,'2015-12-14 09:38:33.691');
INSERT INTO Tabel1 VALUES (6,2,3,2,2,'2015-12-14 09:39:30.305');
INSERT INTO Tabel1 VALUES (7,2,4,13,2,'2015-12-14 09:43:13.916');
INSERT INTO Tabel1 VALUES (8,3,1,15,2,'2015-12-11 10:53:00.000');

    SELECT DISTINCT t1.LPSource
,t1.SymbolId
,t1.Volume
,t1.CreatedDate 
FROM Tabel1 t1
    JOIN (
        SELECT LPSource
            ,SymbolId
            ,MAX(CreatedDate) AS CreatedDate
        FROM Tabel1
        GROUP BY LPSource
            ,SymbolId
        ) t2 on t2.LPSource = t1.LPSource AND t2.SymbolId = t1.SymbolId AND t2.CreatedDate = t1.CreatedDate

JOIN部分:

SELECT LPSource
        ,SymbolId
        ,MAX(CreatedDate) AS CreatedDate
    FROM Tabel1
    GROUP BY LPSource
        ,SymbolId

获取最新的LPSource和SymbolId。然后使用此临时表(t2)中的所有列加入初始表,为您提供所需的结果.t1.LPSource,t1.SymbolId,t1.Volume,t1.CreatedDate