4
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 我如何总结这些列

4

2 回答 2

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
于 2013-07-17T07:38:05.373 回答
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;
于 2013-07-17T07:40:17.397 回答