在复杂查询中使用简单查询会产生错误“无效的列名”

时间:2019-03-29 18:06:54

标签: sql-server

当我运行以下查询时没有错误,但是当我在复杂查询中使用同一查询时,我得到了错误

  

无效的列名“ BillPaidAmount”

运行简单且没有任何错误的查询:

select 
    DateOfPayment,
    isnull(sum(BillPaidAmount), 0) 
from 
    SupplyInvoicePaymentHistory 
group by 
    DateOfPayment

产生错误“无效的列名'BillPaidAmount'”的复杂查询:

with Income (DateSold, Income) as 
(
     select 
         DateSold, isnull(sum(TotalBill), 0)
     from 
         SalesInvoice 
     group by 
         DateSold
),
SupplierPayments (DateOfPayment, BillPaidAmount) as
(
     select 
         DateOfPayment, isnull(sum(BillPaidAmount), 0) 
     from 
         SupplyInvoicePaymentHistory 
     group by 
         DateOfPayment
),
Expensis (Date, Amount) as
(
    select 
        Date, isnull(sum(Amount), 0) 
    from 
        GeneralExpense 
    group by Date
), 
t as 
(
    select 
        i.DateSold, e.Date, sp.DateOfPayment, i.income, e.Amount, 
        sum(isnull(i.income, 0) - (isnull(e.Amount, 0) + isnull(sp.BillPaidAmount, 0))) over (order by i.DateSold, e.Date, sp.DateOfPayment) as closing_balance 
    from 
        income i 
    full outer join 
        expensis e on e.Date = i.DateSold 
    full outer join 
        SupplierPayments sp on sp.DateOfPayment = i.DateSold
) 
select 
    m.DateSold, m.Date, m.DateOfPayment,
    isnull(m.opening_balance, 0) as Opening_Balance,
    isnull(m.Income, 0) as Income,
    isnull(m.Amount, 0) as Expensis,
    isnull(m.closing_balance, 0) as Closing_Balance
from 
    (select 
         DateSold, Date, DateOfPayment, 
         lag(closing_balance, 1, 0) over (order by DateSold, Date, DateOfPayment) as opening_balance,
         Income, Amount, closing_balance,
         BillPaidAmount 
     from 
         t) as m 

我遇到了错误

  

无效的列名“ BillPaidAmount”

上面代码的最后一行,即

select 
    DateSold, Date, DateOfPayment, 
    lag(closing_balance,1,0) over (order by DateSold, Date, DateOfPayment) as opening_balance,
    Income, Amount, closing_balance, BillPaidAmount 
from t

1 个答案:

答案 0 :(得分:0)

如果正确设置代码格式,很容易看到错误原因。我在表't'中没有看到任何名为BillPaidAmount的列

with Income( DateSold, Income ) as (
    select DateSold,isnull(sum(TotalBill),0)
    from SalesInvoice group by DateSold)
, SupplierPayments( DateOfPayment,BillPaidAmount ) as(
    select DateOfPayment,isnull(sum(BillPaidAmount),0) 
    from SupplyInvoicePaymentHistory group by DateOfPayment
), Expensis( Date, Amount ) as(
    select Date ,isnull(sum(Amount),0) 
    from GeneralExpense group by Date
), t as (
    select i.DateSold
        ,e.Date
        ,sp.DateOfPayment
        ,i.income
        , e.Amount
        , sum(isnull(i.income,0)-(isnull(e.Amount,0)+isnull(sp.BillPaidAmount,0))) over (order by i.DateSold,e.Date,sp.DateOfPayment) as closing_balance 
    from income i 
    full outer  join expensis e on e.Date = i.DateSold 
    full outer join SupplierPayments sp on sp.DateOfPayment=i.DateSold
) 
select m.DateSold
    ,m.Date
    ,m.DateOfPayment
    ,isnull(m.opening_balance,0) as Opening_Balance
    ,isnull(m.Income,0) as Income,isnull(m.Amount,0) as Expensis
    ,isnull(m.closing_balance,0) as Closing_Balance
from (
    select DateSold
        ,Date
        ,DateOfPayment
        ,lag(closing_balance,1,0) over (order by DateSold, Date,DateOfPayment) as opening_balance,Income
        , Amount,closing_balance
        ,BillPaidAmount 
    from t
) as m