日期 部门 业绩
2008.01 A 10
2008.01 B 10
2008.01 C 5
2009.12 A 8
2009.12 B 9
2010.07 A 8
··· ··· ···
写出相应的SQL,得到以下结果集:
部门 2008.01 2009.12 2010.07 ···
A 10 8 8 ···
B 10 9 0 ···
C 5 0 0 ···
··· ··· ··· ··· ···
求大神给解答或思路
7 个解决方案
#1
是不确定有多少日期? 日期是动态的、?
#2
传统写法
with t as
(select '2008.01' rq,'A' bm,10 yj from dual union all
select '2008.01' rq,'B' bm,10 yj from dual union all
select '2008.01' rq,'C' bm, 5 yj from dual union all
select '2009.12' rq,'A' bm, 8 yj from dual union all
select '2009.12' rq,'B' bm, 9 yj from dual union all
select '2010.07' rq,'A' bm, 8 yj from dual)
select bm,
max(case when rq='2008.01' then yj else 0 end) "2008.01",
max(case when rq='2009.12' then yj else 0 end) "2009.12",
max(case when rq='2010.07' then yj else 0 end) "2010.07"
from t
group by bm;
#3
pivot 行转列
with t as
(select '2008.01' rq,'A' bm,10 yj from dual union all
select '2008.01' rq,'B' bm,10 yj from dual union all
select '2008.01' rq,'C' bm, 5 yj from dual union all
select '2009.12' rq,'A' bm, 8 yj from dual union all
select '2009.12' rq,'B' bm, 9 yj from dual union all
select '2010.07' rq,'A' bm, 8 yj from dual)
select *
from t
pivot
(
max(yj) --聚合操作函数
for rq --行转列标准
in ('2008.01','2009.12','2010.07') --行转列列取值和顺序
);
#4
写烂了的行转列
#5
嗯,百度一下行转列,大把的文章。
#6
sum(decode......)
自行百度
自行百度
#7
额,我们好像没怎么写烂,汗
#1
是不确定有多少日期? 日期是动态的、?
#2
传统写法
with t as
(select '2008.01' rq,'A' bm,10 yj from dual union all
select '2008.01' rq,'B' bm,10 yj from dual union all
select '2008.01' rq,'C' bm, 5 yj from dual union all
select '2009.12' rq,'A' bm, 8 yj from dual union all
select '2009.12' rq,'B' bm, 9 yj from dual union all
select '2010.07' rq,'A' bm, 8 yj from dual)
select bm,
max(case when rq='2008.01' then yj else 0 end) "2008.01",
max(case when rq='2009.12' then yj else 0 end) "2009.12",
max(case when rq='2010.07' then yj else 0 end) "2010.07"
from t
group by bm;
#3
pivot 行转列
with t as
(select '2008.01' rq,'A' bm,10 yj from dual union all
select '2008.01' rq,'B' bm,10 yj from dual union all
select '2008.01' rq,'C' bm, 5 yj from dual union all
select '2009.12' rq,'A' bm, 8 yj from dual union all
select '2009.12' rq,'B' bm, 9 yj from dual union all
select '2010.07' rq,'A' bm, 8 yj from dual)
select *
from t
pivot
(
max(yj) --聚合操作函数
for rq --行转列标准
in ('2008.01','2009.12','2010.07') --行转列列取值和顺序
);
#4
写烂了的行转列
#5
嗯,百度一下行转列,大把的文章。
#6
sum(decode......)
自行百度
自行百度
#7
额,我们好像没怎么写烂,汗