行转列

--创建临时表,将查出的数据插入临时表
declare @tm_staticTable table(children int,tdtname varchar(255),stand_tdt varchar(255),cycle varchar(255));
insert into @tm_staticTable (children,tdtname,stand_tdt,cycle) select a.children,a.tdtname,b.stand_tdt,a.cycle
from '+@staticTable+' as a left join E3_VERSION_TDT_MAP as b on a.tdtname = b.EA_TDT where a.tdtname <> '''' 

and b.EA_VERSION = '''+@version+'''


--行转列

declare @tem_calss table(id int identity(1,1) primary key,name001 varchar(255),class001 varchar(255),number001 numeric(5,2))


insert into @tem_calss values ('熊敏','语文',100)
insert into @tem_calss values ('熊敏','数学',100)
insert into @tem_calss values ('熊敏','物理',100)
insert into @tem_calss values ('熊伟','语文',100)
insert into @tem_calss values ('熊伟','数学',100)

insert into @tem_calss values ('熊伟','物理',100)


select name '姓名',
max(case course when '语文' then number else 0 end) '语文',
max(case course when '数学' then number else 0 end) '数学',
max(case course when '物理' then number else 0 end) '物理'
from @tem_calss group by name

--sql2005中行转列关键词

select * from @tem_calss pivot(max(number) for course in ('语文','数学','物理')) a



--列传行
declare @tem_tablele table (姓名 varchar(10),语文 int,数学 int,物理 int)


insert into @tem_tablele values ('熊敏',100,100,100)
insert into @tem_tablele values ('熊伟',100,100,100)
insert into @tem_tablele values ('熊无名',100,100,100)


select * from @tem_tablele


select * from
(
 select 姓名,'语文' 课程,语文 分数 from @tem_tablele
 union all
 select 姓名,'数学' 课程,数学 分数 from @tem_tablele
 union all
 select 姓名,'物理' 课程,物理 分数 from @tem_tablele
) t
order by 姓名


--查看长度

print len(@RunSQL)


--分页

String sql = "select top 10 * from shangPin where changPinID not in (select top "+10*(page-1)+" changPinID from shangPin order by changPinID desc) order by changPinID desc";


你可能感兴趣的:(行转列)