-1

我想要做的是将多行选择到临时表的单行中。临时表中应该有 1 行列出所有不同的应计值。下面是代码:

    declare @RunDate varchar(7)
set @RunDate = '2013-07'

declare @ShortItemNo1 int

set @ShortItemNo1 = 723639




declare @SalesAccruals table(QuantityShipped int, ShortItemNo int, GrossAmount money, AccrualReturns money, AccrualMedicaid money,
                            AccrualChargebacks money)

insert @SalesAccruals

select
    sum(s.QuantityShipped) QuantityShipped,
    s.ShortItemNo,
    sum(s.ExtendedPrice) ExtendedPrice,
    case when aa.AccrualType=3 then sum(a.AccrualAmount) end,
    case when aa.AccrualType=8 then sum(a.AccrualAmount) end,
    case when aa.AccrualType=2 then sum(a.AccrualAmount) end
from
    SalesSummary s join Accruals a on
        s.SalesSummaryGuid = a.SalesSummaryGuid
    join AccrualsSetup aa on
        a.AccrualsSetupGuid = aa.AccrualsSetupGuid
    join LookupAccrualTypes la on
        la.AccrualTypeID = aa.AccrualType

where
    convert(varchar(7), InvoiceDate, 20) = @RunDate
    and s.ShortItemNo in (@ShortItemNo1)
group by
    s.ShortItemNo,
    aa.AccrualType

select * from @SalesAccruals
4

1 回答 1

0

尝试将SELECT查询的部分更改为...

select
    sum(s.QuantityShipped) QuantityShipped,
    s.ShortItemNo,
    sum(s.ExtendedPrice) ExtendedPrice,
    sum(case when aa.AccrualType=3 then a.AccrualAmount else 0 end),
    sum(case when aa.AccrualType=8 then a.AccrualAmount else 0 end),
    sum(case when aa.AccrualType=2 then a.AccrualAmount else 0 end)

然后aa.AccrualTypeGROUP BY.

于 2013-10-24T15:21:24.597 回答