A. sql把查询出的一个列转换成行
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b
B. sql语句怎么把列变成行
create table rotatetable1 (序号 int,company char(66),box_weight char(12),废塑料numeric(10,2)),废五金 numeric(10,2)),废钢铁 numeric(10,2)),废纸 numeric(10,2)),废有色 numeric(10,2)),废纤维 numeric(10,2)),其它 numeric(10,2)),合计 numeric(10,2)));
insert into rotatetable1(company,box_weight) select name ,'weight' from sum1 group by name;
insert into rotatetable1(company,box_weight) select name ,'box' from sum1 group by name;
update rotatetable1 set 废塑料=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='废塑料';
update rotatetable1 set 废塑料=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='废塑料';
::: :::
update rotatetable1 set 其它=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='其它';
update rotatetable1 set 其它=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='其它';
::: :::
update rotatetable1 set 合计=废塑料+废五金+废钢铁+废纸+废有色+废纤维+其它;
(所有涉及表的行列转换均可按照这种方式实现。)
C. sql语句列转行
我整理的行转列的问题:
--创建tb表
createtabletb(姓名varchar(10),课程varchar(10),分数int)
insertintotbvalues('张三','语文',74)
insertintotbvalues('张三','数学',83)
insertintotbvalues('张三','物理',93)
insertintotbvalues('李四','语文',74)
insertintotbvalues('李四','数学',84)
insertintotbvalues('李四','物理',94)
go
select*Fromtb
--SQLSERVER2000静态行转列
select姓名as姓名,
max(case课程when'语文'then分数elsenullend)语文,
max(case课程when'数学'then分数elsenullend)数学,
max(case课程when'物理'then分数elsenullend)物理
fromtb
groupby姓名
--SQLSERVER2000动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
declare@sqlvarchar(8000)
set@sql='select姓名'
select@sql=@sql+',max(case课程when'''+课程+'''then分数else0end)['+课程+']'
from(selectdistinct课程fromtb)asa
set@sql=@sql+'fromtbgroupby姓名'
exec(@sql)
--SQLSERVER2005静态SQL。
select*from(select*fromtb)apivot(max(分数)for课程in(语文,数学,物理))b
--SQLSERVER2005动态SQL。
declare@sqlvarchar(8000)
select@sql=isnull(@sql+'],[','')+课程fromtbgroupby课程
set@sql='['+@sql+']'
exec('select*from(select*fromtb)apivot(max(分数)for课程in('+@sql+'))b')
希望对你的学习有帮助。
D. SQL行转列,列转行
行列转换在做报表分析时还是经常会遇到的,今天就说一下如何实现行列转换吧。
行列转换就是如下图所示两种展示形式的互相转换
假如我们有下表:
通过上面 SQL 语句即可得到下面的结果
PIVOT 后跟一个聚合函数来拿到结果,FOR 后面跟的科目是我们要转换的列,这样的话科目中的语文、数学、英语就就被转换为列。IN 后面跟的就是具体的科目值。
当然我们也可以用 CASE WHEN 得到同样的结果,就是写起来麻烦一点。
使用 CASE WHEN 可以得到和 PIVOT 同样的结果,没有 PIVOT 简单直观。
假设我们有下表 student1
通过 UNPIVOT 即可得到如下结果:
我们也可以使用下面方法得到同样结果
E. sql怎么将列转换成一行
selectordernum,
[values]=stuff((select'/'+[列名]fromtbtwhereordernum=tb.ordernumforxmlpath('')),1,1,'')
fromtb
groupbyordernum
你这是一个查询吧,第二列连列名都没有
sqlserver2005以上版本支持,tb是表名,自己换一下
F. 如何用sql语句 把列 转换成 一行 结果
1、最原始的方法,利用 select....union
select 栏位1 from 表名 where 条件 union select 栏位2 from 表名 where 条件 select 栏位3 from 表名 where 条件 ......
2、利用 SYS_CONNECT_BY_PATH函数
SELECT SYS_CONNECT_BY_PATH(栏位, '>') "Path"
FROM 表名
START WITH 条件
CONNECT BY PRIOR 条件;
G. SQL表列数据转成行数据
create table #A03(AId varchar(30),
CardNo varchar(30),SumMoney money,Type varchar(30)) --临时表
create table #A07(AId varchar(30),
CardNo varchar(30),SumMoney money,Type varchar(30))
insert into #A03 select AId,CardNo,SumMoney,Type from A
where AID='B6BA' and CardNo='996000010003'
insert into #A70 select AId,CardNo,SumMoney,Type from A
where AID='B6BA' and CardNo='996000010070'
select a.AID,a.CardNo,a.SumMoney,a.Type,
b.CardNo,b.SumMoney,b.Type
from #A03 a right join #A70 b on a.CardNo='996000010003' and b.CardNo='996000010070'
--你试试
H. sql 最简单的列转行
oracle中列传行可用wm_concat来实现。
如test表中数据如下:
现要将name列一列显示成行,可用如下语句:
select wm_concat(name) from test;结果:
I. sql 查询出一列内容,如何让它成一行显示。
进行行转列下面是相关语法等
PIVOT 用于将列值旋转为列名(即行转列),在 SQL Server 2000可以用聚合函数配合CASE语句实现
PIVOT 的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P
注意:PIVOT、UNPIVOT是SQL Server 2005 的语法,使用需修改数据库兼容级别(在数据库属性->选项->兼容级别改为 90 )
SQL2008 中可以直接使用
完整语法:
table_source
PIVOT(
聚合函数(value_column)
FOR pivot_column
IN(<column_list>)
)
View Code
UNPIVOT 用于将列明转为列值(即列转行),在SQL Server 2000可以用UNION来实现
完整语法:
table_source
UNPIVOT(
value_column
FOR pivot_column
IN(<column_list>)
)