简体   繁体   English

SQL max(DateTime)和类别过滤器,不按分组

[英]SQL max(DateTime) and category filter without group by

I've written the below query : 我写了下面的查询:

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

This gives the following output: 这给出以下输出:

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

What I need of this output is the following: per rowId and configId combination I need the highest value from the datetime column. 我需要的输出如下:每个rowId和configId组合我需要datetime列中的最大值。 So from above example I want the following output: 因此,从上面的示例中,我需要以下输出:

DateTime           configId    rowId
12-09-2013 12:00     4           12
12-09-2013 13:00     3           11
12-09-2013 11:00     4           11

Does anyone know the answer? 有人知道答案吗? I would like to avoid GROUP BY because the select statement will be extended with a lot more columns. 我想避免使用GROUP BY,因为select语句将扩展更多的列。

Thanks in advance 提前致谢

EDIT The current query: 编辑当前查询:

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

You could do this, substituting proper joining fields as appropriate: 您可以这样做,并适当地替换适当的联接字段:

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

Dont know much about the columns in your table but it would be something like this.... Query in CROSS APPLY with get the highest TOP 1 date for every result and then feed into the result set or outer query.. 对表中的列了解不多,但是类似这样。...在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM