我写了以下问题:
SELECT DateTime, configId, rowId
FROM linkedTableDefinition a,
INNER JOIN tableDefinition b,
ON a.Target = b.Id
INNER JOIN ViewWithInfo c,
ON a.Target = c.Id
这给出了以下输出:
DateTime configId rowId
12-09-2013 11:00 4 12
12-09-2013 12:00 4 12
12-09-2013 13:00 3 11
12-09-2013 12:00 3 11
12-09-2013 11:00 4 11
我需要的输出如下:每个rowId和configId组合我需要datetime列中的最高值。所以从上面的例子我想要以下输出:
DateTime configId rowId
12-09-2013 12:00 4 12
12-09-2013 13:00 3 11
12-09-2013 11:00 4 11
有谁知道答案?我想避免使用GROUP BY,因为select语句将扩展为更多列。
提前致谢
修改 当前查询:
SELECT testResults.ResultDate, testResults.ConfigurationId, TestResultsTestCaseId
FROM dbo.FactWorkItemLinkHistory workItemLink
INNER JOIN dbo.DimWorkItem workItem
ON workItem.System_Id = workItemLink.TargetWorkItemID
INNER JOIN dbo.TestResultView testResults
ON testResults.TestCaseId = workItemLink.TargetWorkItemID
WHERE
RemovedDate = convert(datetime, '9999-01-01 00:00:00.000')
AND workItemLink.SourceWorkItemID = 7
AND workItem.System_WorkItemType = 'Test Case'
答案 0 :(得分:3)
SELECT *
FROM
(
SELECT DateTime, configId, rowId,
ROW_NUMBER() OVER(PARTITION BY configId, rowId ORDER BY DateTime DESC) AS RowNum
FROM linkedTableDefinition a
INNER JOIN tableDefinition b ON a.Target = b.Id
INNER JOIN ViewWithInfo c ON a.Target = c.Id
) src
WHERE src.RowNum = 1
答案 1 :(得分:0)
您可以这样做,在适当的情况下替换正确的加入字段:
SELECT testResults.ResultDate, testResults.ConfigurationId, testResults.TestCaseId,
(SELECT MAX(ResultDate) FROM dbo.TestResultView WHERE TestCaseId = testResults.TestCaseId AND ConfigurationId = testResults.ConfigurationId) AS MaxDate
FROM dbo.FactWorkItemLinkHistory workItemLink
INNER JOIN dbo.DimWorkItem workItem
ON workItem.System_Id = workItemLink.TargetWorkItemID AND workItemLink.TeamProjectCollectionSK = workItem.TeamProjectCollectionSK
INNER JOIN dbo.TestResultView testResults
ON testResults.TestCaseId = workItemLink.TargetWorkItemID
WHERE
RemovedDate = convert(datetime, '9999-01-01 00:00:00.000')
AND workItemLink.SourceWorkItemID = 7
AND workItem.System_WorkItemType = 'Test Case'
AND workItem.System_RevisedDate = convert(datetime, '9999-01-01 00:00:00.000')
答案 2 :(得分:0)
不太了解你桌子上的列,但它会是这样的.... 在CROSS APPLY中查询,获取每个结果的最高TOP 1日期,然后输入结果集或外部查询..
SELECT configId, rowId, tbl.DateTime
FROM linkedTableDefinition a,
INNER JOIN tableDefinition b,
ON a.Target = b.Id
INNER JOIN ViewWithInfo c,
ON a.Target = c.Id
CROSS APPLY
(
SELECT TOP 1 DateTime
FROM whatevertable
WHERE whatevertable.CommonCol = TabeFrmOuterQuery.CommonCol
ORDER BY DateTime DESC
) tbl