【问题标题】:How to fix “Only one expression can be specified in the select list when the subquery is not introduced with EXISTS” error?如何解决“当子查询未引入 EXISTS 时,选择列表中只能指定一个表达式”错误?
【发布时间】:2015-03-14 05:26:05
【问题描述】:

我的查询如下,其中包含一个子查询:

select a.bill_prvdr_acct_id, a.acct_id, a.bill_prvdr_acct_strt_dt, a.bill_prvdr_acct_end_dt
from xxxx_snapshot.dbo.bill_prvdr_acct_history a
where a.acct_id in 
(select acct_id, count(*)
from xxxx_snapshot.dbo.bill_prvdr_acct_history 
group by acct_id
having count(*) > 1)

我收到错误消息“当 EXISTS 未引入子查询时,选择列表中只能指定一个表达式。”

【问题讨论】:

  • 去掉 count(*) 作为选择值。
  • 你用的是什么数据库?

标签: sql exists


【解决方案1】:

鉴于您的查询结构,窗口函数可能是一种更简单的方法来满足您的需求:

select a.bill_prvdr_acct_id, a.acct_id, a.bill_prvdr_acct_strt_dt, a.bill_prvdr_acct_end_dt
from (select a.*, count(*) over (partition by acct_id) as cnt
      from xxxx_snapshot.dbo.bill_prvdr_acct_history a
     ) a
where cnt > 1;

【讨论】:

    【解决方案2】:

    Count(*) 在子查询的 SELECT 子句中不是必需的,仅在 HAVING 子句中是必需的 - 以下应该有效:

    select a.bill_prvdr_acct_id, a.acct_id, a.bill_prvdr_acct_strt_dt, a.bill_prvdr_acct_end_dt
    from xxxx_snapshot.dbo.bill_prvdr_acct_history a
    where a.acct_id in 
    (select acct_id
    from xxxx_snapshot.dbo.bill_prvdr_acct_history 
    group by acct_id
    having count(*) > 1)
    

    【讨论】:

      【解决方案3】:

      错误是由于在子查询中选择了多个列。

      当您在where 子句中使用IN 运算符时,您不能在subquery 中选择多于一列。

      如错误中所述,使用EXISTS 或从subquery 中删除不需要的count(*)

      SELECT a.bill_prvdr_acct_id,
             a.acct_id,
             a.bill_prvdr_acct_strt_dt,
             a.bill_prvdr_acct_end_dt
      FROM   xxxx_snapshot.dbo.bill_prvdr_acct_history a
      WHERE  EXISTS (SELECT 1
                     FROM   xxxx_snapshot.dbo.bill_prvdr_acct_history b
                     WHERE  a.acct_id = B.acct_id
                     GROUP  BY acct_id
                     HAVING Count(*) > 1) 
      

      或者做一个Inner Join

      SELECT a.bill_prvdr_acct_id,
             a.acct_id,
             a.bill_prvdr_acct_strt_dt,
             a.bill_prvdr_acct_end_dt
      FROM   xxxx_snapshot.dbo.bill_prvdr_acct_history a
             INNER JOIN (SELECT acct_id
                         FROM   xxxx_snapshot.dbo.bill_prvdr_acct_history b
                         GROUP  BY acct_id
                         HAVING Count(*) > 1) B
                     ON a.acct_id = B.acct_id 
      

      【讨论】:

        【解决方案4】:

        为了简化一切,首先独立运行查询以确保返回一列:

        select acct_id
        from xxxx_snapshot.dbo.bill_prvdr_acct_history 
        group by acct_id
        having count(*) > 1
        Above you are returning the acct_id
        

        如果返回所需的结果,请将其包含在主查询中:

        SELECT a.bill_prvdr_acct_id,
               a.acct_id,
               a.bill_prvdr_acct_strt_dt,
               a.bill_prvdr_acct_end_dt
        FROM   xxxx_snapshot.dbo.bill_prvdr_acct_history a
        WHERE  a.acct_id IN (select acct_id
        from xxxx_snapshot.dbo.bill_prvdr_acct_history 
        group by acct_id
        having count(*) > 1) 
        

        这样可以很容易地确认子查询是否返回单个列以及所需的结果并将其链接到主查询将很容易在主查询中出现问题时很容易

        【讨论】:

          猜你喜欢
          • 2013-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多