
正文
sqlserver行列倒置,sql server 行列反转
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
sqlserver行列转换
--把你的那个表写在内存里 你截图不好写
with tbname as(你上面的语句)
--两个语句一起执行
--有什么问题可以随时找我 希望采纳
select etxm,1,2,3,4,5,6,11 from tb pivot(sum(分数) for 课程 in(1,2,3,4,5,6,11)) as t22
select etxm,MAX(case m_no when 1 then rec_other else null end) 1,
MAX(case m_no when 2 then rec_other else null end) 2,
MAX(case m_no when 3 then rec_other else null end) 3,
MAX(case m_no when 4 then rec_other else null end) 4,
MAX(case m_no when 5 then rec_other else null end) 5,
MAX(case m_no when 6 then rec_other else null end) 6,
MAX(case m_no when 11 then rec_other else null end) 11
from tbname
group by etxm
相关问答
Q1: sqlserver将一行数据转为一列显示,怎么实现
select * from
(
select id as Name , Subject = 'id' , Result = id from tb1
union all
select id as Name , Subject = 'yytc' , Result = yytcfrom tb1
) t
order by name , case Subject when 'id' then 1 when 'yytc' then 2 end
没有数据库,没法给你测试,你用这个试试,看好不好使
如果不好使,可以访问这个地址:
里边有行转列,列转行,应该有你想要的,你看看
Q2: sql server 2005中,如何将行列转换?
create table test1(星期 varchar(10),日期 datetime,一班 varchar(10),二班 varchar(10))
insert into test1
select '星期二', '2012-5-1 0:00','张,王',' 李,赵 '
union all
select ' 星期三','2012-5-2 0:00','李,赵','张,王 '
union all
select ' 星期四 ','2012-5-3 0:00','张,王','李,赵' union all
select '星期五','2012-5-4 0:00','李,赵','张,王' union all
select '星期六','2012-5-5 0:00','张,王','李,赵' union all
select '星期日','2012-5-6 0:00','李,赵','张,王' union all
select '星期一','2012-5-7 0:00','张,王','李,赵' union all
select '星期二','2012-5-8 0:00','李,赵','张,王' union all
select '星期三','2012-5-9 0:00','ddd','李,赵'
因为sqlserver pivot 函数转换的时候 都是转换一列,你这个要转3个列,因为也要把日期给转上去。 所以 就先 把你的源表分开。分成 临时表1( 日期,星期,一班)、临时表2(日期,星期,二班)。等两个都转换好了之后 再union 。 下班是我转的 第一。只把一班的转过来了。二班的 类似就行了,到时候一班二班union的时候 按照日期排序,所以都先保留了日期。。 比较复杂,暂时没有想到好的办法。。。。。。
select *,ROW_NUMBER() over(order by 日期 asc) row,case when 星期一 is not null then 日期 end 星期一1,
case when 星期二 is not null then 日期 end 星期二1,
case when 星期三 is not null then 日期 end 星期三1,
case when 星期四 is not null then 日期 end 星期四1,
case when 星期五 is not null then 日期 end 星期五1,
case when 星期六 is not null then 日期 end 星期六1,
case when 星期日 is not null then 日期 end 星期日1
into #a
from (
select * from (
select 星期,日期,一班 from test1
) a
pivot (
max(一班)
for 星期 in (星期一,星期二,星期三,星期四,星期五,星期六,星期日)
)pvt
) a
select * from (
select row,日期,星期一,星期二,星期三,星期四,星期五,星期六,星期日 from #a
union all
select ROW,日期,convert(varchar(10),星期一1,120 ),convert(varchar(10),星期二1,120 ),convert(varchar(10),星期三1,120 ),convert(varchar(10),星期四1,120 ),convert(varchar(10),星期五1,120 ),convert(varchar(10),星期六1,120 ),convert(varchar(10),星期日1,120 ) from #a
)a order by 1
Q3: SQLServer的行列转换,就拿自己在用的业务的表试了下,结果总是如下错误,请大神解答错在哪里。
CAST(SFXM AS varchar(20))
如果还出错,
CAST(CAST(SFXM AS FLOAT) AS varchar(20))
试下吧,数据类型好像是需要转为 float 才能再转为 字符串类型
Q4: SqlServer 行列转换
目前除了povit没什么别的好方法,你也可以去report server 里面的矩阵看看,这个东西性能平时很少有人关注
关于sqlserver行列倒置和sql server 行列反转的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






