# 这样的显示需求，可以通过SQL语句或者过程实现不！解决思路(2)

www.MyException.Cn  网友分享于：2013-01-25  浏览：10次

insert into tb(item,f1,f2,f3,f4) values( '项目1 ', '200602 ', 0, 0, 0)
insert into tb(item,f1,f2,f3,f4) values( '项目1 ', '200603 ', 2, 3, 4)
insert into tb(item,f1,f2,f3,f4) values( '项目1 ', '200604 ', 3, 3, 0)
insert into tb(item,f1,f2,f3,f4) values( '项目2 ', '200601 ', 0, 0, 0)
insert into tb(item,f1,f2,f3,f4) values( '项目2 ', '200602 ', 0, 0, 0)
insert into tb(item,f1,f2,f3,f4) values( '项目2 ', '200603 ', 2, 2, 2)
insert into tb(item,f1,f2,f3,f4) values( '项目2 ', '200604 ', 3, 3, 3)
insert into tb(item,f1,f2,f3,f4) values( '项目3 ', '200601 ', 1, 1, 1)
insert into tb(item,f1,f2,f3,f4) values( '项目3 ', '200602 ', 0, 0, 0)
insert into tb(item,f1,f2,f3,f4) values( '项目3 ', '200603 ', 0, 0, 0)
insert into tb(item,f1,f2,f3,f4) values( '项目3 ', '200604 ', 3, 2, 1)
select * from
(
select item,
max(case when f1 = '200601 ' then f2 end) as '200601 ',
max(case when f1 = '200602 ' then f2 end) as '200602 ',
max(case when f1 = '200603 ' then f2 end) as '200603 ',
max(case when f1 = '200604 ' then f2 end) as '200604 '
from tb
group by item
union all
select item,
max(case when f1 = '200601 ' then f3 end) as '200601 ',
max(case when f1 = '200602 ' then f3 end) as '200602 ',
max(case when f1 = '200603 ' then f3 end) as '200603 ',
max(case when f1 = '200604 ' then f3 end) as '200604 '
from tb
group by item
union all
select item,
max(case when f1 = '200601 ' then f4 end) as '200601 ',
max(case when f1 = '200602 ' then f4 end) as '200602 ',
max(case when f1 = '200603 ' then f4 end) as '200603 ',
max(case when f1 = '200604 ' then f4 end) as '200604 '
from tb
group by item
) t
order by item

drop table tb

item 200601 200602 200603 200604
---------- ----------- ----------- ----------- -----------

（所影响的行数为 9 行）