I have this code:
declare @ReportLines table
(RebateInvoiceID int,
RebateSetupID int ,
ShortItemNo float primary key(RebateInvoiceID,RebateSetupI
You can make another Nesty query... Something Like this:
insert @ReportLines
select
RebateInvoiceID
, ID
, ShortItemNo
, TotalAmount
, sum(round(TotalAmount,2)) as InvoiceTotal
, TotalQuantity
from
(
select
i.RebateInvoiceID
, coalesce(rs.WholesalerRebateSetupID,r.RebateSetupID) as ID
, bl.ShortItemNo
, sum(round(r.Amount,2)) as TotalAmount
, sum(r.Quantity) TotalQuantity
from (... <the rest of your code here> ...)
) As MyTable
group by
RebateInvoiceID, ID, ShortItemNo, TotalAmount, TotalQuantity