求sql高手
table:
user_name | user_num
----------------------
张三 | 1
张三 | 2
张三 | 2
张三 | 2
张三 | 1
张三 | 3
张三 | 3
李四 | 1
李四 | 1
李四 | 3
李四 | 3
李四 | 2
李四 | 2
李四 | 1
要出来的效果:
name | num1 | num2 | num3
---------------------------
张三 | 2 | 3 | 2
李四 | 3 | 2 | 2
说明下:
结果表中的num1表示张三这个人在table表中的user_num等于1的出现了几次
(table表中出现了2次【张三 | 1】,结果表中的效果就是2),num2也是在table表中的user_num等于2的出现了几次,同理num3。李四和张三同理。
select distinct(t.PJR),t.EMP_NAME,count(t.C_ctomer_answer),
(select distinct(t.PJR),count(t.C_ctomer_answer)
from view_tagdm73_mzpy t
where t.C_ctomer_answer = '2'
group by t.PJR,t.EMP_NAME),
(select distinct(t.PJR),count(t.C_ctomer_answer)
from view_tagdm73_mzpy t
where t.C_ctomer_answer = '3'
group by t.PJR,t.EMP_NAME)
from view_tagdm73_mzpy t
where t.C_ctomer_answer = '1'
group by t.PJR,t.EMP_NAME
这个是我想出来的sql语句,但总是出too many values,求高手告诉我怎么修改,或者写个更高端的! 跪求了!~
------解决方案--------------------
SQL code
select user_name name,
sum(decode(user_num,1,1,0)) num1 ,
sum(decode(user_num,2,1,0)) num2,
sum(decode(user_num,3,1,0)) num3
from table_test group by user_name