0

我写了以下查询:

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'
4

3 回答 3

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
于 2013-10-06T16:17:43.827 回答
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
于 2013-10-06T16:22:58.920 回答
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')
于 2013-10-06T16:13:56.213 回答