Domanda

What I am trying to do is select multiple rows into a single row of a temp table. There should be 1 row in the temp table listing all of the different accrual values. Here is the code below:

    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
È stato utile?

Soluzione

Try changing the SELECT part of your query to be this...

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)

Then remove aa.AccrualType from the GROUP BY.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top