如何从内部查询返回多个列来修改查询以解决错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从内部查询返回多个列来修改查询以解决错误相关的知识,希望对你有一定的参考价值。

我有一个复杂的查询,我的内部查询生成正确的结果。但是当我的子查询返回多于列时,我得到错误“Only one expression can be specified in the select list when the sub query is not introduced with EXISTS

我应该如何修改查询以解决错误,但我需要相同数量的内部查询列。

SQL Server 2012查询

select Distinct 
   s.SalesInvoiceID,
   cust.CustomerID,
   cust.Name,
   cust.FName,
   cust.CustomerCNIC,   

   CASE WHEN s.SpecialInsttPlan = 'No' 

   THEN 
        (s.TotalBill - s.Advance) / s.Installments
   ELSE
   (
        select Distinct Top 1 sip.Amount,iph.InsttNo
        from
        SpecialInsttPlan sip 
        join InstallmentPaymentHistory iph
        on iph.InsttNo=sip.InsttNo
        where
        sip.SalesInvoiceID=45 and iph.SalesInvoiceID=45 and 
        (iph.Status ='Pending' or iph.Status ='Up Coming') order by iph.InsttNo
    )

   END as Installment,
   s.TotalBill - s.Advance - sum(iph.Amount)   as BalanceAmount

from
   SalesInvoice s 
   inner join
      InstallmentPaymentHistory iph 
      on iph.SalesInvoiceID = s.SalesInvoiceID 
   inner join
      Customer cust 
      on s.CustomerID = cust.CustomerID 
where
   iph.SalesInvoiceID = 45
group by
   s.SalesInvoiceID,
   s.TotalBill,
   s.Installments,
   s.Discount,
   s.Advance,
   cust.Name,
   cust.FName,
   cust.CustomerID,
   cust.CustomerCNIC,
   s.SpecialInsttPlan,
   iph.InsttNo
答案
select a.SalesInvoiceID,a.CustomerID,a.Name,a.FName,a.CustomerCNIC,

    CASE WHEN a.SpecialInsttPlan = 'No' 

    then 
        a.Installment
    else ( select sip.Amount from SpecialInsttPlan sip 
            where  a.Installment=sip.InsttNo and sip.SalesInvoiceID=44
          ) 
            end as Installment,a.BalanceAmount
from 

(
select Distinct
   s.SalesInvoiceID,
   cust.CustomerID,
   cust.Name,
   cust.FName,
   cust.CustomerCNIC,
   s.SpecialInsttPlan,

   CASE WHEN s.SpecialInsttPlan = 'No' 

   THEN 
        (s.TotalBill - s.Advance) / s.Installments 
   ELSE
   (
         select Distinct Top 1 iph.InsttNo
         from
            SpecialInsttPlan sip 
            join InstallmentPaymentHistory iph
            on iph.InsttNo=sip.InsttNo
         where
        sip.SalesInvoiceID=44 and iph.SalesInvoiceID=44 and 
        (iph.Status ='Pending' or iph.Status ='Up Coming')   order by iph.InsttNo


    )
   END as Installment, 
   s.TotalBill - s.Advance - sum(iph.Amount)   as BalanceAmount

from
   SalesInvoice s 
   inner join
      InstallmentPaymentHistory iph 
      on iph.SalesInvoiceID = s.SalesInvoiceID 
   inner join
      Customer cust 
      on s.CustomerID = cust.CustomerID 
where
   iph.SalesInvoiceID = 44
group by
   s.SalesInvoiceID,
   s.TotalBill,
   s.Installments,
   s.Discount,
   s.Advance,
   cust.Name,
   cust.FName,
   cust.CustomerID,
   cust.CustomerCNIC,
   s.SpecialInsttPlan) as a

因为内部只能返回一列,所以我使用了另一个外部查询,并在内部查询中只使用了一列

以上是关于如何从内部查询返回多个列来修改查询以解决错误的主要内容,如果未能解决你的问题,请参考以下文章

ORA-01427:单行子查询返回多个行

如何构建和查询 Room DB 以返回多个类的对象列表?

如何进行内部联接查询以获取多个选定的表列

如何在 MySql 中使用 sequelize 运行多个原始查询?

SQL 从带有子查询的多个表中选择数据(包括来自内部连接的数据)错误:1242

在单个查询中使用多个联接