2
的另一总和我有这样的代码一栏:总表的基础上表
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
我想从总量列总和发票总金额列。当我运行上述查询时,我得到了一堆奇怪的总数。发票总额应该是每行一个数字,并与rebateinvoiceid一致,因为那是什么它的分组,正确?
不要我不得不加入那些不知何故? – Jeff 2012-07-23 18:44:04
你可以请进入有关该查询的更多细节?我仍然有问题。 – Jeff 2012-07-23 19:32:51
当然,也许你需要给父查询(我的或第一你想看到它)的别名(我已经编辑我的帖子)。这个想法是使用查询作为子查询来进行查询,因为它是一个表,因此您可以从查询中已有的字段中进行选择。如果你想完成所有的查询告诉我,我认为你只需要完成你的查询,在那里说'... <你的代码的其余部分> ... ...' – Elwi 2012-07-23 22:24:31