例表如下:姓名 部门 职级 工资等级张三 1 A 01
李四 2 A 02
王五 2 B 02
赵六 1 A 03
钱七 2 B 02如何能实现下列表示: 职级 A 职级B
部门名 工资等级 01 02 03 工资等级 01 02 03
1 1人 0人 1人 0人 0人 0人
2 0人 1人 0人 0人 2人 0人能够用分组来实现吗?
李四 2 A 02
王五 2 B 02
赵六 1 A 03
钱七 2 B 02如何能实现下列表示: 职级 A 职级B
部门名 工资等级 01 02 03 工资等级 01 02 03
1 1人 0人 1人 0人 0人 0人
2 0人 1人 0人 0人 2人 0人能够用分组来实现吗?
nvl(count(case when salary_status = '01' and status = 'A' then 1),0) salary_status01A,
nvl(count(case when salary_status = '02' and status = 'A' then 1),0) salary_status02A,
nvl(count(case when salary_status = '03' and status = 'A' then 1),0) salary_status03A,
nvl(count(case when salary_status = '01' and status = 'B' then 1),0) salary_status01B,
nvl(count(case when salary_status = '02' and status = 'B' then 1),0) salary_status02B,
nvl(count(case when salary_status = '03' and status = 'B' then 1),0)
salary_status03B
from t
group by department;
2 union all select 'bb',2,'A','02' from dual
3 union all select 'cc',2,'B','02' from dual
4 union all select 'dd',1,'A','03' from dual
5 union all select 'ee',2,'B','02' from dual
6 )
7 select department,
8 nvl(count(case when sLevel = '01' and pLevel = 'A' then 1 end),0) sLevel01A,
9 nvl(count(case when sLevel = '02' and pLevel = 'A' then 1 end),0) sLevel02A,
10 nvl(count(case when sLevel = '03' and pLevel = 'A' then 1 end),0) sLevel03A,
11 nvl(count(case when sLevel = '01' and pLevel = 'B' then 1 end),0) sLevel01B,
12 nvl(count(case when sLevel = '02' and pLevel = 'B' then 1 end),0) sLevel02B,
13 nvl(count(case when sLevel = '03' and pLevel = 'B' then 1 end),0) sLevel03B
14 from tt
15 group by department;DEPARTMENT SLEVEL01A SLEVEL02A SLEVEL03A SLEVEL01B SLEVEL02B SLEVEL03B
---------- ---------- ---------- ---------- ---------- ---------- ----------
1 1 0 1 0 0 0
2 0 1 0 0 2 0
参考下。看看能否自己写出来。你的需求要多一层循环
能靠自己的力量写出来最好,不行的话给下字段名,有空帮你写下
在看NBA直播...呵呵
sqlstr varchar2(4000):='create or replace view v_temp as
select 部门';
cursor cur(zhiji in varchar2) is
select distinct 工资等级 from test where 职级=zhiji;
begin
for cur1 in (select distinct 职级 from test)loop
for cur2 in cur(cur1.职级) loop
sqlstr:=sqlstr||chr(10)||',count(case when 职级='''||cur1.职级||''' and 工资等级='''||cur2.工资等级||''' then 1 end)"'||cur1.职级||cur2.工资等级||'"';
end loop;
end loop;
sqlstr:=sqlstr||chr(10)||'from test group by 部门';
--dbms_output.put_line(sqlstr);
execute immediate sqlstr;
end;select * from v_temp;部门 A01 A02 A03 B02
1 1 0 1 0
2 0 1 0 2
你的例子中的字段名比较难处理,我改成这种形式,应该也差不多吧。其中职级B只有一个B02,你需要的话也可以把01和03补齐,但是因为你的职级数和工资等级数并不确定,如果不确定的话,那些字段是莫须有的,补齐有点别扭
sqlstr varchar2(4000):='create or replace view v_temp as
select 部门';
cursor cur(zhiji in varchar2) is
select distinct 工资等级 from test where 职级=zhiji;
begin
for cur1 in (select distinct 职级 from test)loop
for cur2 in cur(cur1.职级) loop
sqlstr:=sqlstr||chr(10)||',count(case when 职级='''||cur1.职级||''' and 工资等级='''||cur2.工资等级||''' then 1 end)"'||cur1.职级||cur2.工资等级||'"';
end loop;
end loop;
sqlstr:=sqlstr||chr(10)||'from test group by 部门';
--dbms_output.put_line(sqlstr);
execute immediate sqlstr;
end;select * from v_temp;部门 A01 A02 A03 B02
1 1 0 1 0
2 0 1 0 2