请教如何通过SQL语句将表1的内容转为表2的内容,感谢!!表1:
MC TD ZT
a 001 完成
b 001 未完成
c 001 未完成
d 002 完成
r 002 完成
f 002 完成
g 003 未完成表2
TD MC数量 ZT已完成状态
001 3 1
002 3 3
003 1 0
MC TD ZT
a 001 完成
b 001 未完成
c 001 未完成
d 002 完成
r 002 完成
f 002 完成
g 003 未完成表2
TD MC数量 ZT已完成状态
001 3 1
002 3 3
003 1 0
from 表1
group by TD
insert into tab select
'a','001','完成' union all select
'b','001','未完成' union all select
'c','001','未完成' union all select
'd','002','完成' union all select
'r','002','完成' union all select
'f','002','完成' union all select
'g','003','未完成'select TD,count(MC) as [MC数量],ZT已完成状态=sum(case when ZT='完成' then 1 else 0 end) from tab group by [TD]
/*
TD MC数量 ZT已完成状态
---------- ----------- -----------
001 3 1
002 3 3
003 1 0(所影响的行数为 3 行)*/
select Td,count(*) as MC数量,sum(case when zt='完成' then 1 else 0 end) as ZT已完成状态
from 表1
group by TD
小改一下.
select Td,count(*) as MC数量,SUM(case when zt='完成' then 1 ELSE 0 end) as ZT已完成状态
from 表1
group by TD