首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

*数据表怎么换化显示有关问题*

2012-02-12 
***数据表如何换化显示问题***原如表结果IDTYPElength5309622DT30.008002619BR15.001002619CO30.001002619

***数据表如何换化显示问题***
原如表结果
 ID TYPE length
5309622DT30.00
8002619BR15.00
1002619CO30.00
1002619BR30.00


需要转化成,列名无所谓


 5309622 CA100 -----(固定值)
  DT 30.00
 8002619 CA100
  BR 15.00
 1002619 CA100 -----有2条相同ID,应该显示如下
  CO 30.00
  BR 30.00

[解决办法]

SQL code
create table tb(ID varchar(10),TYPE varchar(10),length decimal(10,2))insert into tb select '5309622','DT',30.00insert into tb select '8002619','BR',15.00insert into tb select '1002619','CO',30.00insert into tb select '1002619','BR',30.00go;with c1 as(select row_number()over(order by (select 1))rn,id,'CA100' as col from (select distinct id from tb)t),c2 as(select a.rn,b.type,convert(varchar(10),b.length)col from c1 a inner join tb b on a.id=b.id)select * from c1 union all select * from c2 order by rn/*-------------------- ---------- ----------1                    1002619    CA1001                    CO         30.001                    BR         30.002                    5309622    CA1002                    DT         30.003                    BR         15.003                    8002619    CA100(7 行受影响)*/godrop table tb 

热点排行
Bad Request.