我一直試圖做的SQL操作中很多隻是爲了以後但這兩天仍然沒有喜悅得到期望的結果...SQL結果顯示重複的列值
我有下面的SQL結果和我試圖實現「Desired Result」表。它所做的是總結基於每個分支的Stype的NoScan的價值。
Code Branch NoScan sType
PK001 BF 258 R
PK001 BF 474 N
BO001 BF 435 N
MM006 BF 62 R
LH001 CP 2 F
RK001 CP 1 O
QB001 CP 26 N
TJ001 CP 3 F
GS147 DU 79 O
HR001 DU 5 F
IV002 DU 3 F
NP123 DU 149 O
WC001 EL 30 R
CO100 EL 230 R
CO100 EL 4 F
Desired Result
Branch F(Count) R(Count) N(Count) O(Count)
BF 0 320 909 0
CP 5 0 26 1
DU 8 0 0 228
EL 435 260 0 0
這是我的基本查詢,但我不包括我想出了其他的查詢,因爲他們沒有無論如何都要得到期望的結果。
;with deli as
(
SELECT
c.Code as Code
,dh.Branch as Branch
,COUNT(dh.dID) as NoScan
,dh.sType as sType
FROM dbo.tblSDHead dh with (nolock)
inner join dbo.tblCor c with (nolock) on dh.dID = c.code
inner join dbo.tblSPD spd with(nolock) on dh.wbno = spd.wbno
WHERE
dh.Branch IN
(
SELECT DISTINCT code
FROM tblloc
WHERE coid IN ('1','2')
)
and c.isactive = 1
and c.sshd = 1
and spd.pddate >= @startdate
and spd.pddate < @enddate
and spd.delicomp = 'Y'
and dh.sType in ('N','O','R','F')
GROUP BY
c.Code
,dh.branch
,dh.sType
)
select
*
from
(
select
o.Code
,o.Branch
,o.NoScan
,o.sType
from
deli o
) overall
order by overall.branch asc
任何幫助,非常感謝!由於
哇靠它的工作!我做了同樣的事情,但是當stype ='F'然後求和(NoScan)else 0時,我把這個總和放在case這個case裏面。但是它當然不起作用。非常感謝你! – dimas