我试图让这个查询总结客户收费不包括税。在这种情况下,客户费用和税金称为i.cht_itemcode(VARCHAR(5)),它位于Invoicedetail表中。 原来我使用LEFT JOIN在Invoicedetail表,这是不对的带来。 Orderheader表的值o.ord_charge与ivd_charge的值相同,其中cht_itemcode是LHF。case语句来排除特定值SUM
我结束了书面方式那段这种方式来代替,并移除左连接。而不是试图在查询本身做SUM,我决定拆分2种不同的收费金额,并在SSRS中进行SUM。
CASE WHEN o.ord_invoicestatus='AVL'
THEN o.ord_charge
ELSE (select sum(i.ivd_charge) from invoicedetail i
where i.cht_itemcode='LHF' and i.ord_hdrnumber=o.ord_hdrnumber)
END as 'Revenue',
(SELECT SUM(I.IVD_CHARGE) from invoicedetail i where
i.ord_hdrnumber=o.ord_hdrnumber and i.cht_itemcode not in ('LHF','HST2','TAX3','GST','BCCT'))'Accessorial'
你可以把'CASE'在'SUM',但不知道这是什么你问..'SUM(CASE WHEN cht_itemcode IN( 'HST2', 'TAX3', 'BCCT')THEN 0 ELSE i.IVD_CHARGE END)' – JamieD77