跪求sql server2012行转列方案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
下面为表创建代码:
create table [dbo].[productauditrecord]([parid] [nchar](12)
not null,[moid] [nchar](12)
not null,[lotsn] [nvarchar](50)
not null,[cosmeticinspection] [nchar](12)
not null,[functionaltest] [nchar](12)
not null,[unumber] [nchar](50)
null,[leadwire] [nchar](50)
null,[resourceid] [nchar](12)
not null,[userid] [nchar](12)
not null,[remark] [nvarchar](100)
null,[creatdate] [datetime] not null,[productid] [nchar](12)
not null,[nextid] [int] not null, constraint [pk_productauditrecord] primary key clustered ([parid] asc)with
(pad_index = off, statistics_norecompute = off,
ignore_dup_key = off, allow_row_locks = on,
allow_page_locks = on) on [primary])
on [primary]goalter table [dbo].[productauditrecord] add
constraint [df_productauditrecord_parid] default
(substring(convert([char](36),
newid(),(0)),(1),(12)))
for [parid]goalter table [dbo].
[productauditrecord] add constraint
[df_productauditrecord_nextid] default
((0))
for [nextid]go
下面为自己测试数据得到的结果:上面为原始数据,下面为转换后的数据:转换代码测试表代码:create table [dbo].[test](
[月份] [varchar](4) null,
[工资] [int] null,
[福利] [int] null,
[奖金] [int] null
) on [primary]
1:月份工资福利奖金
1月100 200 300
2月110 210 310
3月120 220 320
4月130 230 330
2:考核月份1月2月3月4月
福利200 210 220 230
工资100 110 120 130
奖金300 310 320 330
select * from
(
select 考核月份,月份,金额from
(select 月份,工资,福利,奖金from test) p unpivot
(金额for 考核月份in (工资,福利,奖金))as unpvt ) t
pivot
(max(金额) for 月份in ([1月],[2月],[3月],[4月]))as pt 更多信息请查看IT技术专栏