Ⅰ 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>)
)
Ⅱ SQL语句 列转行
-- ========================= PIVOT 行列转置 ===========================
-- 1、【行列转置PIVOT】
declare @Score table(StuNo varchar(10), StuName varchar(50), CourseName varchar(50), Score int)
insert into @Score
select '1', 'Tom', 'Math', 80 union all
select '1', 'Tom', 'English', 82 union all
select '1', 'Tom', 'Geography', 84 union all
select '2', 'Jone', 'Math', 79 union all
select '2', 'Jone', 'English', 88 union all
select '2', 'Jone', 'Geography',86
select * from @Score
SELECT StuNo, StuName, Math, English, [Geography]
FROM @Score PIVOT (MAX(Score) FOR CourseName in (Math, English, [Geography]) ) AS ScoreList
ORDER BY StuNo
-- 2、【列行转置UNPIVOT】
declare @ScoreList table(StuNo varchar(10), StuName varchar(50), Math int, English int, [Geography] int)
insert into @ScoreList
select '1', 'Tom', 80, 82, 84 union all
select '2', 'Jone', 79, 88, 86
select * from @ScoreList
SELECT StuNo, StuName, CourseName, Score
FROM @ScoreList UNPIVOT (Score FOR CourseName in (Math, English, [Geography]) ) AS ScorePvtTable
ORDER BY StuNo
Ⅲ SQL 查询 表格的转换,将列转换成行显示
SELECTSN,SUM(CASEWHENField_Name='BL1_Ver'THENField_ValueEND)BL1_Ver,
SUM(CASEWHENField_Name='BL2'THENField_ValueEND)BL2,
SUM(CASEWHENField_Name='BL3'THENField_ValueEND)BL3
FROM表名
GROUPBYSN
Ⅳ sql 最简单的列转行
oracle中列传行可用wm_concat来实现。
如test表中数据如下:
现要将name列一列显示成行,可用如下语句:
select wm_concat(name) from test;结果:
Ⅳ 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'
--你试试
Ⅵ 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 合计=废塑料+废五金+废钢铁+废纸+废有色+废纤维+其它;
(所有涉及表的行列转换均可按照这种方式实现。)