我一直试图做很多sql操作只是为了得到理想的结果,但两天之后仍然没有快乐......
我有下面的SQL结果,并且正在尝试实现"期望的结果"表。它的作用是根据Stype per Branch总结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
非常感谢任何帮助!感谢
答案 0 :(得分: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
将tablename替换为生成输入的子查询(派生表)。
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分:0)
这似乎是一种错误的做法。如果您的sType列中有超过4个值,则需要更新查询。应该在表示层处理显示。
您应该尝试使用固定数量的列显示相同的结果,如下所示:
select branch, sType, sum(NoScan)
from table
group by branch, sType