在子查询中使用group by

时间:2018-06-18 17:21:04

标签: sql sql-server

执行时我有以下这一行,给我这个错误。我如何使用此处存在过滤行'next_NotedID'?在此先感谢

 Msg 116, Level 16, State 1, Line 27
    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

这是我的where子句

 where (Name = 'File Review' and next_Name = 'File Review')
                                                 and   (TYPECODE ='1200005' and next_Typecode = '1200005')
                                                 and   ((HEADLINE like '%Follow up letter%')
                                                        and (next_Headline like '%Follow up letter%'))
                                                  and next_NotedID <>  (SELECT  ParentRef, max(NoteID)from Stg.IG_Note group by PARENTREF)  

2 个答案:

答案 0 :(得分:1)

您可以将subquery相关联

. . .
and next_NotedID <> (SELECT max(nt.NoteID) 
                     from Stg.IG_Note nt 
                     where nt.PARENTREF = outerqueryalise.ParentRef
                     );  

如果您与内部查询next_NotedID的{​​{1}}进行比较,那么您可以直接将其表达为

max(NoteID)

答案 1 :(得分:0)

更改此行:

and next_NotedID <>  (SELECT  ParentRef, max(NoteID)from Stg.IG_Note group by PARENTREF)

为:

and next_NotedID <>  (SELECT  max(NoteID)from Stg.IG_Note group by PARENTREF) 

使用&lt;&gt;时,您无法在子选择中选择多于1个字段。如果这会返回多于一个可能的值,则需要将其更改为NOT IN而不是&lt;&gt;