select
UnsubmittedCount=(
case
when
ApproveStatusin
('未提交'
,
'已提交'
)
then
count
(FormID)
end
),
ApprovedCount=(
case
when
ApproveStatus
in
(
'已审核'
)
then
count
(FormID)
end
)
from
formLogistic
group
by
ApproveStatus
返回结果集为GroupBy后的多行数据,如何避开GroupBy?
可以使用max()或者sum()的方式,将casewhen一行化,如:
select
UnsubmittedCount=
max
(
case
when
ApproveStatus
in
(
'未提交'
sql plain" style="list-style:none; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'已提交'
)
then
count
(FormID)
end
)
from
formLogistic
group
by
ApproveStatus
可以优化为:
select
UnsubmittedCount=
COUNT
(
case
when
ApproveStatus
in
(
'未提交'
sql plain" style="list-style:none; font-size:1em!important; line-height:1.1em!important; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'已提交'
)
then
FormID
end
sql plain" style="list-style:none; font-size:1em!important; line-height:1.1em!important; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ApprovedCount=
COUNT
'已审核'
then
FormID
)
formLogistic