SQL表行列转换后得到新的查询数据

我有一张sql表如下CodeNameProductS201101S201102S201103S201104XQ0001AAP12040500XQ0001AAP232307... 我有一张sql表如下
Code Name Product S201101 S201102 S201103 S201104
XQ0001 AA P1 20 40 50 0
XQ0001 AA P2 32 3 0 78
XQ0002 BB P1 32 34 9 3
XQ0002 BB P2 3 3 5 3
-------------------------------------------------------------------------------
我现在想显示为
Year Code Name P1 P2

S201101 XQ0001 AA 20 32
S201102 XQ0001 AA 40 3
S201103 XQ0001 AA 50 0
S201104 XQ0001 AA 0 78
S201101 XQ0002 BB 32 3
S201102 XQ0002 BB 34 3
S201103 XQ0002 BB 9 5
S201104 XQ0002 BB 3 3
展开
 我来答
厍曼冬6o
2013-05-17 · TA获得超过1569个赞
知道小有建树答主
回答量:1336
采纳率:0%
帮助的人:1202万
展开全部
select S201101 year,Code,Name,case when Product=P1 then S201101 else 0 end P1,case when Product=P2 then S201101 else 0 end P2 from table1
union all
select S201102 year,Code,Name,case when Product=P1 then S201102 else 0 end P1,case when Product=P2 then S201102 else 0 end P2 from table1
union all
select S201103 year,Code,Name,case when Product=P1 then S201103 else 0 end P1,case when Product=P2 then S201103 else 0 end P2 from table1
union all
select S201104 year,Code,Name,case when Product=P1 then S201104 else 0 end P1,case when Product=P2 then S201104 else 0 end P2 from table1
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
thain0
2013-05-17 · TA获得超过267个赞
知道小有建树答主
回答量:597
采纳率:0%
帮助的人:528万
展开全部
select 'S201101' as year, code, name, product as P1, S201101 as P2
 from table
union all
select 'S201102' as year, code, name, product as P1, S201102 as P2
union all
select 'S201103' as year, code, name, product as P1, S201103 as P2
union all
select 'S201104' as year, code, name, product as P1, S201104 as P2
更多追问追答
追问
这个代码不对的
追答
select A.year, A.code, A.name, sum(A.P1) P1, sum(A.P2) P2

from
(

select 'S201101 'as year, code,name, S201101 as P1, null as P2
from table where product = 'P1'

unionall
select 'S201102' as year, code,name, S201102 as P1, null as P2
from table where product = 'P1'
unionall
select 'S201103' as year, code,name, S201103 as P1, null as P2
from table where product = 'P1'
unionall
select 'S201104' as year, code,name, S201104 as P1, null as P2
from table where product = 'P1'
union all
select 'S201101 'as year, code,name, null as P1 , S201101 as P2

from table where product = 'P2'

unionall
select 'S201102' as year, code,name, null as P1 , S201102 as P2
from table where product = 'P2'
unionall
select 'S201103' as year, code,name, null as P1 , S201103 as P2
from table where product = 'P2'
unionall
select 'S201104' as year, code,name, null as P1 , S201104 as P2
from table where product = 'P2'
) A
Group by A.year, A.code, A.name
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jiangzhijie628
2013-05-17 · TA获得超过113个赞
知道答主
回答量:180
采纳率:0%
帮助的人:90万
展开全部
你这个用三层循环就能搞定 分别对S201101(2,3,4...),code,P1(2,3,4,...)循环
更多追问追答
追问
具体怎么做?
追答
for(i=1,i<4,i++)
for(j=1,j<2,j++)
for(n=1,n<2,n++)
select S20110+i as year, XQ000+j as code,name, product as P+n
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式