足球游戏_中国足彩网¥体育资讯$

跪求sql server2012行转列方案
来源:易贤网 阅读:775 次 日期:2014-12-22 15:30:26
温馨提示:易贤网小编为您整理了“跪求sql server2012行转列方案”,方便广大网友查阅!

下面为表创建代码:

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技术专栏

中国足彩网信息请查看数据库
易贤网手机网站地址:跪求sql server2012行转列方案
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标