列转行

怎么把一条记录拆分成几条记录?
User     No.         A           B            C
1        1           21          34           24
1        2           42          25           16

RESULT:

User     No.        Type       Num
1        1          A          21
1        1          B          34
1        1          C          24
1        2          A          42
1        2          B          25
1        2          C          16

不好意思,没有多少分了,只好给20分

 

---sql server 2005
declare @t table(usser int ,no int ,a int,b int, c int)
insert into @t select 1,1,21,34,24
union all select 1,2,42,25,16

SELECT usser,no,Type=attribute, Num=value
FROM @t
  UNPIVOT
  (
    value FOR attribute IN([a], [b], [c])
  ) AS UPV
       
--结果
/*

usser   no       Type      num
----   ---      --------  --------
1 1 a 21
1 1 b 34
1 1 c 24
1 2 a 42
1 2 b 25
1 2 c 16
*/

最近做毕业设计,遇到一个问题要进行行列转换,由于本人水平有限,网上的代码也很难看懂,所以就到这里来求一句代码望哪位大哥大姐能帮帮忙,小弟不胜感激!

原来的数据:
材料费用   人工费用   机械费用  其他费用
  150        260         330       400


要转换成:
费用类型   数目
材料费用   150
人工费用   260
机械费用   330
其他费用   400

 

Select
N'材料费用' As 费用类型 ,
材料费用 As 数目
From TableName
Union All
Select
N'人工费用' As 费用类型 ,
人工费用 As 数目
From TableName
Union All
Select
N'机械费用' As 费用类型 ,
机械费用 As 数目
From TableName
Union All
Select
N'其他费用' As 费用类型 ,
其他费用 As 数目
From TableName

 

--如果不知列名,用動態語句
Declare @S Nvarchar(4000)
Set @S=''
Select @S=@S+'Select N'''+Name+N''' As 费用类型 ,'+Name+N' As 数目 From TEST Union All '
From SysColumns Where ID=OBJECT_ID('TEST') Order By ColID
Select @S=Left(@S,Len(@S)-10)
EXEC(@S)

--建立測試環境
Create Table TEST(
材料费用 Int,
人工费用 Int,
机械费用   Int,
其他费用 Int)
Insert TEST Select   150,        260        , 330       ,400
GO
--測試

--已知列名
Select
N'材料费用' As 费用类型 ,
材料费用 As 数目
From TEST
Union All
Select
N'人工费用' As 费用类型 ,
人工费用 As 数目
From TEST
Union All
Select
N'机械费用' As 费用类型 ,
机械费用 As 数目
From TEST
Union All
Select
N'其他费用' As 费用类型 ,
其他费用 As 数目
From TEST

--如果不知列名,用動態語句
Declare @S Nvarchar(4000)
Set @S=''
Select @S=@S+'Select N'''+Name+N''' As 费用类型 ,'+Name+N' As 数目 From TEST Union All '
From SysColumns Where ID=OBJECT_ID('TEST') Order By ColID
Select @S=Left(@S,Len(@S)-10)
EXEC(@S)
GO
--刪除測試環境
Drop Table TEST
GO
--結果
/*
费用类型 数目
材料费用 150
人工费用 260
机械费用 330
其他费用 400
*/

 

--来个动态的
create table tb(材料费用 int,人工费用 int,机械费用 int,其他费用 int)
insert into tb select 150,260,330,400
go

declare @sql varchar(8000)
set @sql=''
select @sql=@sql+' union all select ,'''+name+''' as 费用类型,'+name+' as 数目 from tb ' from syscolumns where id=object_id('tb') order by colid
set @sql=replace(@sql,'union all select ,','union all select ')
set @sql=stuff(@sql,1,11,'')
exec(@sql)

drop table tb

 

--上面的写的有点问题,太繁琐了.修改下
create table tb(材料费用 int,人工费用 int,机械费用 int,其他费用 int)
insert into tb select 150,260,330,400
go

declare @sql varchar(8000)
set @sql=''
select @sql=@sql+' union all select '''+name+''' as 费用类型,'+name+' as 数目 from tb ' from syscolumns where id=object_id('tb') order by colid
set @sql=stuff(@sql,1,11,'')
exec(@sql)

drop table tb

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2006/06/15/426241.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值