代码错误当子查询没有引入exists时,只能在选择列表中指定一个表达式

hmtdttj4  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(268)

我试图运行这个查询,但无论我做什么,我不断得到这个错误,我无法解决它。任何帮助都将不胜感激。

select
  CC.ContactID, 

from 
  Client C 
  join Contacts CC on CC.ContactID = C.ContactID 
 where 

        ClientID In (

                                  Select 
                                    ClientID,Sum(Total-allocated) as Bal 
                                  from 
                                    Main 
                                  Where 
                                    Total <> Allocated
                                    and NomTypeID < 10 
                                  Group By 
                                    ClientID
                                  HAVING Sum(Total-allocated) > 10
                             )
jjhzyzn0

jjhzyzn01#

我不知道你用的是哪种db(所以我也无法测试我的答案)。
但一般来说 IN 您需要子查询只返回一列。
所以查询应该被删除 Sum(Total-allocated) as Bal ):

select
  CC.ContactID, 

from 
  Client C 
  join Contacts CC on CC.ContactID = C.ContactID 
 where 

        ClientID In (

                                  Select 
                                    ClientID 
                                  from 
                                    Main 
                                  Where 
                                    Total <> Allocated
                                    and NomTypeID < 10 
                                  Group By 
                                    ClientID
                                  HAVING Sum(Total-allocated) > 10
                             )
6jjcrrmo

6jjcrrmo2#

请使用下面的查询,

select
  CC.ContactID from  
Client C 
join Contacts CC on CC.ContactID = C.ContactID 
join (Select ClientID,Sum(Total-allocated) as Bal from Main Where 
Total <> Allocated and NomTypeID < 10 
Group By ClientID HAVING Sum(Total-allocated) > 10)) m
on (C.ClientID = m.ClientID);

相关问题