如何在最大日期和最大ID

时间:2016-11-17 14:57:51

标签: sql sql-server

这是我的SQL表

RecordID      RecordState     Time
-----------------------------------
record1       failed          1:00
record1       passed          2:00

record2       passed          2:15

record3       failed          3:00

record4       passed          4:00
record4       failed          5:00

我只需选择'失败'记录。

select RecordID, max(RecordState)
from RecordTable a
inner join RecordStateTable b
on a.recordID = b.recordID
group by RecordID

这会提取所有失败记录,但此处的问题是 record4 ,其中值首先传递然后失败,因此传递此记录的max(recordstate)。我不知何故也需要为Max(时间)修改它,但我不知道如何。

预期结果为record3(failed),record4(failed)

6 个答案:

答案 0 :(得分:0)

不是选择可能导致非常混乱的结果的最大状态(因为它只是字符串) - 您可以为每个max(Time)选择RecordID,然后仅记录最大时间状态失败的记录:

select T1.*
from
(
    select
        RecordID,
        max(Time) as Time
    from RecordStateTable 
    group by RecordID
) as T
    inner join RecordStateTable as T1 on T1.RecordID = T.RecordID and T1.Time = T.Time
where T1.RecordState = 'failed'

答案 1 :(得分:0)

您可以使用分析(窗口函数)获取每个recordID的最大时间,并仅显示失败状态下的最长记录时间。

with CTE AS (
Select RecordID, RecordState, Time, max(time) over (partition by RecordID) mt
FROM table)
Select * from cte where mt = time and recordState = 'failed'

答案 2 :(得分:0)

尝试这样的事情

Select * From
(
select *,
       row_number()over(partition by RecordID order by [time] desc) as rn 
from yourtable
) A
Where RN = 1 and RecordState = 'Failed'

使用Row_number标识每个time的最大RecordID并过滤最长时间以及RecordState = 'Failed'

答案 3 :(得分:0)

假设您想要从每条记录中返回最新结果:

    declare @RecordTable TABLE (
        RecordID varchar(10),
        RecordState varchar(10),
        [Time] time
    )

    insert into @RecordTable
    values
        ('record1', 'failed', '1:00'),
        ('record1', 'passed', '2:00'),
        ('record2', 'passed', '2:15'),
        ('record3', 'failed', '3:00'),
        ('record4', 'passed', '4:00'),
        ('record4', 'failed', '5:00')

    select  a.RecordID, a.RecordState, b.[Time]
    from    @RecordTable a
        inner join (
            select  RecordID, max([Time]) as [Time]
            from    @RecordTable
            group by RecordID
        ) b on b.RecordID = a.RecordID and a.[Time] = b.[Time] -- only return the most recent version

    --record4   failed  05:00:00.0000000
    --record3   failed  03:00:00.0000000
    --record2   passed  02:15:00.0000000
    --record1   passed  02:00:00.0000000

这会获取每条记录的max(Time)并从该行获取状态。如果您只想要失败(或通过)记录,只需添加where a.RecordState = 'whatever'

答案 4 :(得分:0)

这将返回仅有失败状态的最新时间

select RecordID,RecordState row_number
over (partition by recordID
order by Time desc) as rn   from 
yourtable
where RecordState = 'failed' and rn = 1

答案 5 :(得分:0)

我会使用APPLY来做到这一点。

SELECT rt.RecordID, ca.RecordState
FROM RecordTable rt
CROSS APPLY (SELECT TOP 1 RecordState, Time
             FROM RecordTable
             WHERE RecordID = rt.RecordID
             ORDER BY Time DESC) ca
WHERE rt.Time = ca.Time AND ca.RecordState = 'failed'

根据您的查询,我怀疑实际上有两个表。假设RecordID是RecordTable上的PK,您可以将上述内容简化为此。

SELECT rt.RecordID, ca.RecordState
FROM RecordTable rt
CROSS APPLY (SELECT TOP 1 RecordState
             FROM RecordStateTable
             WHERE RecordID = rt.RecordID
             ORDER BY Time DESC) ca
WHERE ca.RecordState = 'failed'