SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A,**sum(WO+WP+HL+A)**
from TRN_ATTN072013 WHERE CONVERT(varchar,Tdate,112)>'20130712' and CONVERT(varchar,Tdate,112)<'20130717'
group by emp_no
ORDER BY emp_no
我想总结一下这些专栏WO,WP,HL,A我如何总结这些专栏
答案 0 :(得分:6)
如果是SQL server
,那么您可以使用CTE
with tablesum as
(
SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A
from TRN_ATTN072013
WHERE CONVERT(varchar,Tdate,112) > '20130712' and CONVERT(varchar,Tdate,112) < '20130717'
group by emp_no
)
select t.*, t.WO + t.WP + t.HL + t.A
from tablesum t
order by t.emp_no
答案 1 :(得分:2)
在mysql中;)
SELECT emp_no, wo+wp+hl+A FROM
(SELECT DISTINCT emp_no, sum(CASE WHEN isnull(PresAbs,0)='WO' THEN 1 ELSE 0 END) WO,
sum(CASE WHEN isnull(PresAbs,0)='WP' THEN 1 ELSE 0 END) WP,
sum(CASE WHEN isnull(PresAbs,0)='HL' THEN 1 ELSE 0 END) HL,
sum(CASE WHEN isnull(PresAbs,0)='A' THEN 1 ELSE 0 END) A,**sum(WO+WP+HL+A)**
from TRN_ATTN072013 WHERE CONVERT(varchar,Tdate,112)>'20130712' and CONVERT(varchar,Tdate,112)<'20130717'
group by emp_no
ORDER BY emp_no) AS SUB_Q;