私はこのコードを持っています:
declare @ReportLines table
(RebateInvoiceID int,
RebateSetupID int ,
ShortItemNo float primary key(RebateInvoiceID,RebateSetupID,ShortItemNo),
TotalAmount float,
InvoiceTotal float,
TotalQuantity int )
insert @ReportLines
select
i.RebateInvoiceID
, coalesce(rs.WholesalerRebateSetupID,r.RebateSetupID)
, bl.ShortItemNo
, sum(round(r.Amount,2)) as TotalAmount
, sum(round(TotalAmount,2)) as InvoiceTotal
, sum(r.Quantity) TotalQuantity
from
@Invoices i
join RebateInvoices ri (nolock) on
ri.RebateInvoiceID=i.RebateInvoiceID
inner loop join Rebates r (nolock) on
r.RebateInvoiceID=i.RebateInvoiceID
join RebateSetup rs (nolock) on
rs.RebateSetupID=r.RebateSetupID
join BidLines bl (nolock) on
r.BidLineGuid=bl.BidLineGuid
join @Products p on
p.ShortItemNo=bl.ShortItemNo
left join ChargebackDetailHistory cd (nolock) on
r.DocumentBranchPlant = cd.DocumentBranchPlant
and r.DocumentNumber = cd.DocumentNumber
and r.DocumentType = cd.DocumentType
and r.LineNumber = cd.LineNumber
left join EDI.dbo.JDE_SaleDetail sd (nolock) on
r.DocumentBranchPlant = sd.BranchPlant
and r.DocumentNumber = sd.OrderNumber
and r.DocumentType = sd.OrderType
and r.LineNumber = sd.LineNumber
where
cd.InvoiceDate between @BeginDate and @EndDate
or sd.InvoiceDate between @BeginDate and @EndDate
group by
i.RebateInvoiceID
, coalesce(rs.WholesalerRebateSetupID,r.RebateSetupID)
, bl.ShortItemNo
合計金額列から請求書合計金額列を合計したい。上記のクエリを実行すると、一連の奇妙な合計が得られます。請求書の合計は、行ごとに 1 つの数値である必要があり、rebateinvoiceid と一致している必要があります。これがグループ化されているためです。正しいですか?