Sql 如何自查询表以便从同一个表中获取最后执行的testcase
我正在使用MSSQL数据库SQL Server 2008 我必须查询数据库Sql 如何自查询表以便从同一个表中获取最后执行的testcase,sql,Sql,我正在使用MSSQL数据库SQL Server 2008 我必须查询数据库 select *, DateDiff(n,StartDateTime,EndDateTime) as DateDifference from result where Project='Sample' AND SubProject='Sample' and (StartDateTime)>='11/01/2013 00:00:00' AND (EndDateTime)<='11/11/2014
select *, DateDiff(n,StartDateTime,EndDateTime) as DateDifference
from result
where Project='Sample' AND SubProject='Sample' and
(StartDateTime)>='11/01/2013 00:00:00' AND (EndDateTime)<='11/11/2014 23:59:59'
order by EndDateTime desc
我想要enries,比如最后执行的测试用例
TestCaseName userName StartDatetime EndDateTime
poonam_tm_003 User1 2014-11-10 17:39:27.000 2014-11-10 17:39:31.000
30SepTestCase TM1 2014-10-29 10:12:09.000 2014-10-29 10:17:07.000
我只需要每个测试用例的最后一个实体。我指的是基于datediff的使用而具有maxEndDateTime的testcase,我假设您使用的是SQL Server。如果是这样,您可以使用row_number执行此操作:
以下是一个可能有帮助的答案:
SELECT TestCaseName,
userName,
StartDatetime,
EndDateTime,
Datediff(N, StartDatetime, EndDateTime) AS datedifference
FROM (SELECT *,
ROW_NUMBER()
OVER (
partition BY testcasename
ORDER BY enddatetime DESC) AS rn
FROM yourtable) AS t
WHERE Project = 'Sample'
AND SubProject = 'Sample'
AND ( StartDateTime ) >= '11/01/2013 00:00:00'
AND ( EndDateTime ) <= '11/11/2014 23:59:59'
AND rn = 1
我希望group by和max能合作
WITH CTE(Testcasename,userName,StartDatetime)
AS
(
SELECT Testcasename,userName,MAX(StartDatetime) StartDatetime
FROM result
GROUP BY Testcasename,Username
)
SELECT A.*,DateDiff(DD,A.StartDateTime,A.EndDateTime) as DateDifference FROM
RESULT A INNER JOIN CTE B ON A.Testcasename=B.Testcasename
AND A.userName=B.userName
AND A.StartDatetime=B.StartDatetime
WHERE
Project='Sample' AND SubProject='Sample' and
(A.StartDateTime)>='11/01/2013 00:00:00' AND (A.EndDateTime)<='11/11/2014 23:59:59'
order by A.EndDateTime desc
请不要在评论中问你的问题。将这几点添加到您的问题中,并使问题更加清楚。您的查询结果是什么。您使用的是哪种DBMS?
SELECT TestCaseName,
userName,
StartDatetime,
EndDateTime,
Datediff(N, StartDatetime, EndDateTime) AS datedifference
FROM (SELECT *,
ROW_NUMBER()
OVER (
partition BY testcasename
ORDER BY enddatetime DESC) AS rn
FROM yourtable) AS t
WHERE Project = 'Sample'
AND SubProject = 'Sample'
AND ( StartDateTime ) >= '11/01/2013 00:00:00'
AND ( EndDateTime ) <= '11/11/2014 23:59:59'
AND rn = 1
WITH CTE(Testcasename,userName,StartDatetime)
AS
(
SELECT Testcasename,userName,MAX(StartDatetime) StartDatetime
FROM result
GROUP BY Testcasename,Username
)
SELECT A.*,DateDiff(DD,A.StartDateTime,A.EndDateTime) as DateDifference FROM
RESULT A INNER JOIN CTE B ON A.Testcasename=B.Testcasename
AND A.userName=B.userName
AND A.StartDatetime=B.StartDatetime
WHERE
Project='Sample' AND SubProject='Sample' and
(A.StartDateTime)>='11/01/2013 00:00:00' AND (A.EndDateTime)<='11/11/2014 23:59:59'
order by A.EndDateTime desc