2017-04-17 161 views
0

我一直試圖做的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 

任何幫助,非常感謝!由於

回答

5

使用case表達式來完成有條件聚集:

select branch, 
     sum(case when sType = 'F' then NoScan else 0 end) as Fcount, 
     sum(case when sType = 'N' then NoScan else 0 end) as Ncount, 
     sum(case when sType = 'R' then NoScan else 0 end) as Rcount, 
     sum(case when sType = 'O' then NoScan else 0 end) as Ocount 
from tablename 
group by branch 

與生成您輸入一個子查詢(派生表)更換表名。

+0

哇靠它的工作!我做了同樣的事情,但是當stype ='F'然後求和(NoScan)else 0時,我把這個總和放在case這個case裏面。但是它當然不起作用。非常感謝你! – dimas

2

使用條件聚集,跳過CTE:

select 
    Branch = dh.Branch 
    , F_Count = sum(case when left(dh.sType,1) = 'F' then 1 else 0 end) 
    , R_Count = sum(case when left(dh.sType,1) = 'R' then 1 else 0 end) 
    , N_Count = sum(case when left(dh.sType,1) = 'N' then 1 else 0 end) 
    , O_Count = sum(case when left(dh.sType,1) = 'O' then 1 else 0 end) 
    , NoScan = count(dh.did) 
    , sType = dh.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 ('ndx','onx','rdf','fdxe') 
group by dh.branch 
+0

非常感謝! – dimas

1

您需要整體後多了一個查詢。

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 ('NDX','ONX','RDF','FDXE') 
     GROUP BY 
      c.Code 
      ,dh.branch 
      ,dh.sType 
    ) 
,overall as 
    (
     select 
      o.Code 
      ,o.Branch 
      ,o.NoScan 
      ,o.sType 
     from 
      deli o 
    ) 
select o.Branch 
     ,sum(case when o.sType = 'F' then o.NoScan else null end) as F_Count 
     ,sum(case when o.sType = 'R' then o.NoScan else null end) as R_Count 
     ,sum(case when o.sType = 'N' then o.NoScan else null end) as N_Count 
     ,sum(case when o.sType = 'O' then o.NoScan else null end) as O_Count 
from overall o 
group by o.Branch 
+0

謝謝傑里米,我會確保注意到這一點 – dimas

0

這似乎是一個錯誤的方法。如果您的sType列中有超過4個值,那麼您需要更新您的查詢。顯示應該在表示層處理。

你應該嘗試用固定數量的列這樣的顯示同樣的結果:

select branch, sType, sum(NoScan) 
from table 
group by branch, sType