我想要做的是将多行选择到临时表的单行中。临时表中应该有 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