2012-07-18 40 views
-3
的SQL最大的datetime

此语句返回2行:获取多行

SELECT ts.UnitId,ts.TeststepId, MAX(ts.CreatedAt)as CreatedAt 
FROM Teststep ts 
INNER JOIN Unit u ON ts.UnitId = u.UnitId 
Where u.TemplateId = 2 
Group by TeststepId, ts.UnitId 

我怎样才能获得一个行最高CreatedAt日期?我应该区分unitId

UPDATE

亚伦和flem aksed更多信息:

1 Template has N Units 
1 Unit has N Teststeps 

的一步步测试表的聚集主键是TeststepId和CreatedAt。

[TeststepId] [int] NOT NULL, 

[Name] [nvarchar](50) NOT NULL, 

[PreCondition] [nvarchar](500) NOT NULL, 

[TestInstruction] [nvarchar](500) NOT NULL, 

[ExpectedResult] [nvarchar](500) NOT NULL, 

[CreatedAt] [datetime] NOT NULL, 

[CreatedBy] [nvarchar](50) NOT NULL, 

[UnitId] [int] NOT NULL, 

与在一步步测试表中的最重要领域的一步步测试

样本数据可能是:

enter image description here

的一步步测试表明历史化teststeps。从上面的图片可以看出TeststepId = 4存在2次。历史版本(58秒)和当前版本(59秒)。

我想只用59秒的时间才能在“当前”视图中显示teststepid 4。

更新2

我想只有teststepid 4 59秒 所有其他teststepId s with their current date. If there are only rows with different teststepid。就拿那些,因为它们是当前行。

我正要改变我的集成测试并找到解决方案。

+0

你可以扩展你实际想要实现的目标吗?例如当有多个单位时会发生什么? – 2012-07-18 16:07:53

+0

同意@flem - 你想要“一排”,还是你想要一排*** PER UNITID ***?你能否指定我们知道我们正在回答的问题,还可以指定SQL Server的版本? – 2012-07-18 16:11:40

+0

@Aaron我更新了我的问题。你的解决方案都可以工作,问题是哪一个更快。通常不会存在超过20个具有相同标识但日期不同的测试步骤。所以我猜orderby子句比CTE快。 – Elisabeth 2012-07-18 17:49:47

回答

2
SELECT TOP (1) ts.UnitId, ts.TeststepId, ts.CreatedAt 
    FROM dbo.Teststep AS ts 
    INNER JOIN Unit AS u 
    ON ts.UnitId = u.UnitId 
    WHERE u.TemplateId = 2 
    ORDER BY ts.CreatedAt DESC; 

如果你想每单元ID一排,和SQL Server 2005 +,则:

;WITH x AS 
(
    SELECT ts.UnitId, ts.TestStepId, ts.CreatedAt, 
    rn = ROW_NUMBER() OVER (PARTITION BY ts.UnitId ORDER BY ts.CreatedAt DESC) 
    FROM dbo.Teststep AS ts 
    INNER JOIN Unit AS u 
    ON ts.UnitId = u.UnitId 
    WHERE u.TemplateId = 2 
) 
SELECT UnitId, TestStepId, CreatedAt 
    FROM x 
    WHERE rn = 1; 
+0

如果有多个单元会怎么样? – 2012-07-18 15:56:26

+0

信息在问题中。 OP需要最高的CreatedAt值。在平局的情况下,没有区别。 – 2012-07-18 16:03:26

+0

我想我们都做出了假设。我假定OP要为每个单元设置最高的“createAt”。你认为OP希望任何单位的最高“createAt”。 – 2012-07-18 16:07:07

0
SELECT TOP 1 x.* FROM 
    (SELECT ts.UnitId,ts.TeststepId, MAX(ts.CreatedAt)as CreatedAt FROM Teststep ts 
     INNER JOIN Unit u ON ts.UnitId = u.UnitId 
     Where u.TemplateId = 2 
     Group by TeststepId, ts.UnitId 
     ORDER BY ts.CreatedAt DESC) as [x] 
+0

如果有多个单位会怎么样? – 2012-07-18 15:56:40

+0

@flem OP只希望返回的一行包含'max(ts.CreatedAt)',这是'ts.CreatedAt'按降序排列时的第一行。 – Widor 2012-07-18 16:02:42

+0

我认为这有点冒失。如果是这样的话,那么答案不需要分组,只需一个简单的'select top 1 ... where ... order by creatat desc' – 2012-07-18 16:05:16

1

另一种选择

试试这个:

WITH Data AS 
(
    SELECT ts.UnitId, ts.TeststepId, ROW_NUMBER() OVER(PARTITION BY UnitId ORDER BY ts.CreatedAt DESC) Position 
     FROM Teststep ts INNER JOIN Unit u ON ts.UnitId = u.UnitId 
    WHERE u.TemplateId = 2 
) 
SELECT * 
    FROM Data 
WHERE Position = 1 
0

另一方式:

with cte as (
SELECT ts.UnitId,ts.TeststepId, MAX(ts.CreatedAt)as CreatedAt 
FROM Teststep ts 
INNER JOIN Unit u ON ts.UnitId = u.UnitId 
Where u.TemplateId = 2 
Group by TeststepId, ts.UnitId) 
select ts.UnitId,ts.TeststepId, CreatedAt from cte where CreatedAt 
=(select MAX(CreatedAt) from cte) 
+1

你应该看看这个声明的执行计划哦,我的...... ;-) – Elisabeth 2012-07-18 15:45:02