SELECT TOP 1 MAX(DATETIME)

时间:2013-02-16 05:03:40

标签: sql sql-server select

假设我有一个名为'@DeviceSample'的表和另一个名为'@DevicesActivityDataSample'.

的表

我希望在每个设备上选择前1 'MAX(DATETIME)' FROM '@DevicesActivityDataSample'当我有重复的行时,在'DevicesActivityDataSample'中我的结果中我得到所有重复的行,但我找的是前一个。 另一个问题是当我想从100000行中选择时需要很长时间才能得到我的结果因为计算MAX这是我的示例代码

DECLARE @DeviceSample TABLE
(
RowID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
ADeviceID INT ,
DeviceName VARCHAR(50) NOT NULL
) 
INSERT @DeviceSample
SELECT 1,'DEVICE 1' UNION ALL
SELECT 2,'DEVICE 2' UNION ALL
SELECT 3,'DEVICE 3'



DECLARE @DevicesActivityDataSample TABLE
(
RowID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
BDeviceID INT,
DeviceDateTime DATETIME,
CMD NVARCHAR(50)
) 

INSERT @DevicesActivityDataSample
SELECT 1, '2013-02-14 19:08:46.000','HHJJ' UNION ALL
SELECT 2, '2013-02-15 03:30:24.000','HHJJ' UNION ALL
SELECT 2, '2013-02-15 03:30:24.000','HHJJ' UNION ALL
SELECT 2, '2013-02-15 03:30:24.000','HHJJ' UNION ALL
SELECT 1, '2013-02-16 03:30:24.000','HHJJ' UNION ALL
SELECT 1, '2013-02-11 03:30:24.000','HHJJ' UNION ALL
SELECT 2, '2013-02-10 03:30:24.000','HHJJ' UNION ALL
SELECT 3, '2013-02-07 04:25:24.000','HHJJ' UNION ALL
SELECT 3, '2013-02-12 23:02:02.000','HHJJ' 



    SELECT  *
    FROM        @DeviceSample
    INNER JOIN  
    @DevicesActivityDataSample 
            ON  ADeviceID = BDeviceID
    WHERE       DeviceDateTime IN (
                    SELECT    MAX(DeviceDateTime) AS MaxDate
                    FROM      @DevicesActivityDataSample                                
                    GROUP BY  BDeviceID
                ) 

GO

如何获得1000000行或更多行的快速快速结果

2 个答案:

答案 0 :(得分:5)

SELECT DeviceID, MAX(DeviceDateTime)
FROM @DeviceSample
GROUP BY DeviceID;

如果这很慢,那么考虑使用#temp表而不是@table变量(see the major differences here),并添加如下索引:

CREATE CLUSTERED INDEX LatestDeviceTime
  ON #DeviceSample (DeviceID, DeviceDateTime DESC);

或者首先不使用表变量或临时表,并将有用的索引添加到数据来源的基表中。

答案 1 :(得分:3)

这是每组查询中最大的n。将表变量的定义更改为

DECLARE @DevicesActivityDataSample TABLE (
  RowID          INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
  BDeviceID      INT,
  DeviceDateTime DATETIME,
  CMD            NVARCHAR(50),
  UNIQUE (BDeviceID, DeviceDateTime DESC, RowID, CMD) ) 

添加有用的索引。

方法1

SELECT DADS.*
FROM   @DeviceSample DS
       CROSS APPLY (SELECT TOP 1 *
                    FROM   @DevicesActivityDataSample DADS
                    WHERE  DADS.BDeviceID = DS.ADeviceID
                    ORDER  BY DADS.DeviceDateTime DESC) DADS;

方法2

WITH T
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY BDeviceID 
                                       ORDER BY DeviceDateTime DESC) AS RN
         FROM   @DevicesActivityDataSample)
SELECT *
FROM   T
WHERE  RN = 1 

Plans

您也可以考虑使用#temp表,因为@Aaron建议可能使用以下定义。

CREATE TABLE #DevicesActivityDataSample (
  RowID          INT IDENTITY(1, 1) PRIMARY KEY NONCLUSTERED,
  BDeviceID      INT,
  DeviceDateTime DATETIME,
  CMD            NVARCHAR(50)) 

CREATE CLUSTERED INDEX IX ON #DevicesActivityDataSample(BDeviceID, DeviceDateTime DESC) 

你真的需要RowID吗?