SQL Server Top 1返回null,前2个工作

时间:2015-06-11 13:09:26

标签: sql sql-server select

以下是代码:

select z.id, z.name, z.duration, z.errors, z.warnings, m.message from
(
    select Id, TestName as Name, Duration, isnull(Errors,0) as Errors, isnull(Warnings,0) as Warnings from 
    (
        select a.id as ID, a.Duration, b.TestName from
        (       
            select * from dbo.test where runid = 20902 and InstrumentUniqueId = 2
        ) as a
            join dbo.testinfo as b  
            on a.testinfoid = b.id      
    ) as x
    left outer join
    (
        select testid,
            sum(case when te.levelid = 1 then 1 else 0 end) as Errors,
            sum(case when te.levelid = 2 then 1 else 0 end) as Warnings
        from dbo.testevent te
        group by testid
    ) as y
    on x.ID = y.testid
) as z
left outer join 
(
    select top (1) message, testid from dbo.testevent where levelid = 1 
) as m
on z.id = m.testid
ORDER BY ID

对于select top (1) message, testid from dbo.testevent where levelid = 1,在消息字段中不会返回任何内容,但是当我将其更改为top (2)时,第一条消息会正确显示。

此外,如果我在大规模联接之外单独运行select top (1) message, testid from dbo.testevent where levelid = 1,则会返回与在大联接中使用top (2)相同的结果。

有没有人经历过这个?

0 个答案:

没有答案