数据库:SQL Server
希望这很简单。我有一个采购订单查询,我试图减去两个案例陈述,但我遇到了让它运作的问题。我正在使用临时表来提取采购订单数据。
请参阅以下查询:
--temp table for PO Inventory lines
select
prchseordr_id,
ISNULL (sum(prchseordrlst_cst_extndd),0) as Inv_Cost
INTO #TempPOInv
from prchseordr
left outer join prchseordrlst on prchseordr.prchseordr_rn = prchseordrlst.prchseordr_rn
where prchseordr_nxt_id = ''
group by prchseordr_id
--------------------
--temp table for PO General lines
select
prchseordr_id,
ISNULL (sum(prchseordrlstgn_cst_extndd),0) as Gen_Cost
INTO #TempPOGen
from prchseordr
left outer join prchseordrlstgn on prchseordr.prchseordr_rn = prchseordrlstgn.prchseordr_rn
where prchseordr_nxt_id = ''
group by prchseordr_id
--------------------
--temp table for PO Subcontractor lines
select
prchseordr_id,
ISNULL (sum(prchseordrlstsb_cntrct_amnt_orgnl),0) as Sub_Cost
INTO #TempPOSub
from prchseordr
left outer join prchseordrlstsb on prchseordr.prchseordr_rn = prchseordrlstsb.prchseordr_rn
where prchseordr_nxt_id = ''
group by prchseordr_id
------------------------------------------------------------
Select
vndr_nme as [Vendor Name],
sum(CASE WHEN prchseordr_type = 'Purchase' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Purchases,
sum(CASE WHEN prchseordr_type = 'Credit' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Credits
from prchseordr
left outer join #TempPOInv on prchseordr.prchseordr_id = #TempPOInv.prchseordr_id
left outer join #TempPOGen on prchseordr.prchseordr_id = #TempPOGen.prchseordr_id
left outer join #TempPOSub on prchseordr.prchseordr_id = #TempPOSub.prchseordr_id
join vndr on prchseordr.vndr_rn = vndr.vndr_rn
where prchseordr.prchseordr_entrd_dte between @DateStart@ and @DateEnd@
and prchseordr_rn <> 0
group by vndr_nme,prchseordr_type
order by vndr_nme
DROP TABLE #TempPOInv
DROP TABLE #TempPOGen
DROP TABLE #TempPOSub
非常基本我只想从积分中减去购买量以获得净购买订单总数。使这项工作的最佳方法是什么?
答案 0 :(得分:1)
有几种方式......一种是CTE
;with cte as(
Select
vndr_nme as [Vendor Name],
sum(CASE WHEN prchseordr_type = 'Purchase' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Purchases,
sum(CASE WHEN prchseordr_type = 'Credit' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Credits
from prchseordr
left outer join #TempPOInv on prchseordr.prchseordr_id = #TempPOInv.prchseordr_id
left outer join #TempPOGen on prchseordr.prchseordr_id = #TempPOGen.prchseordr_id
left outer join #TempPOSub on prchseordr.prchseordr_id = #TempPOSub.prchseordr_id
join vndr on prchseordr.vndr_rn = vndr.vndr_rn
where prchseordr.prchseordr_entrd_dte between @DateStart@ and @DateEnd@
and prchseordr_rn <> 0
group by vndr_nme,prchseordr_type)
select
[Vendor Name],
Purchases,
Credits,
Purchases - Credits as NewColumn
from cte
答案 1 :(得分:1)
观察您的案例陈述 - 您希望根据案例求和。
之后,最可读的是将它放入子查询或CTE中,如:
select
x.[Vendor Name]
,x.Credits - x.Purchases [Profit]
from
(
Select
vndr_nme as [Vendor Name],
sum(CASE WHEN prchseordr_type = 'Purchase' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Purchases,
sum(CASE WHEN prchseordr_type = 'Credit' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Credits
From ...
) x
或者:
;with x as (
Select
vndr_nme as [Vendor Name],
sum(CASE WHEN prchseordr_type = 'Purchase' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Purchases,
sum(CASE WHEN prchseordr_type = 'Credit' THEN Inv_Cost + Gen_Cost + Sub_Cost else 0 end ) Credits
From ...
)
select
x.[Vendor Name]
,x.Credits - x.Purchases [Profit]
from x