当前位置:首页 » 编程语言 » 列转行sql写法
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

列转行sql写法

发布时间: 2022-04-14 02:00:16

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 合计=废塑料+废五金+废钢铁+废纸+废有色+废纤维+其它;

(所有涉及表的行列转换均可按照这种方式实现。)

㈡ sql2008列转行语句

http://blog.csdn.net/greystar/article/details/5660645
看看上面三种方法是不是你需要的。

㈢ sql 列转行

先建一张转换后的表
create table changed
(
id number,
key varchar2(10),
val varchar2(10)
)
然后执行下面的插入语句
insert all
into changed(id,key,val) values(id,'a',a)
into changed(id,key,val) values(id,'b',b)
into changed(id,key,val) values(id,'c',c)
into changed(id,key,val) values(id,'d',d)
into changed(id,key,val) values(id,'e',e)
select id,a,b,c,d,e from change;
如果需要继续添加,就模仿上面的格式写好了,最后别忘记commit;

㈣ sql语句 列变行 行变列

--SQL Server 2005以上版本用unpivot
use Tempdb
go
--> -->

if not object_id(N'Tempdb..#1') is null
drop table #1
Go
Create table #1([区域名称] nvarchar(3),[小类1] int,[小类2] int,[小类3] int)
Insert #1
select N'奎文区',120,100,90
Go
select
[区域名称],[分类],[值]
from
#1
unpivot
([值] for [分类] in([小类1],[小类2],[小类3]))b
/*
区域名称 分类 值
奎文区 小类1 120
奎文区 小类2 100
奎文区 小类3 90
*/

㈤ 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语句列转行

主要应用case语句来解决行转列的问题
行转列问题主要分为两类
1)简单的行转列问题:
示例表:
id
sid
course
result
1
2005001
语文
80.0
2
2005001
数学
90.0
3
2005001
英语
80.0
4
2005002
语文
56.0
5
2005002
数学
69.0
6
2005002
英语
89.0
执行
select
sid,语文=isnull(sum(case
course
when
'语文'
then
result
end),0),
数学=isnull(sum(case
course
when
'数学'
then
result
end),0),
英语=isnull(sum(case
course
when
'英语'
then
result
end),0)
from
result
group
by
sid
order
by
sid
得出结果
sid
语文
数学
英语
2005001
80.0
90.0
80.0
2005002
56.0
69.0
89.0

㈦ 行转列,列转行怎么做sql

/*行转列*/
SELECT*FROM[StudentScores]/*数据源*/ASP
PIVOT
(SUM(Score/*行转列后列的值*/)FOR
p.Subject/*需要行转列的列*/IN([语文],[数学],[英语],[生物]/*列的值*/)
)AST
/*列转行*/
SELECTP.ProgrectName,P.Supplier,P.SupplyNumFROM(SELECTProgrectName,OverseaSupply,NativeSupply,
SouthSupply,NorthSupplyFROMProgrectDetail
)T
UNPIVOT
(
SupplyNumFORSupplierIN
(OverseaSupply,NativeSupply,SouthSupply,NorthSupply)
)P

㈧ SQL语句如何列变行

select 字段名按照你要求的排序就行了

select a,b,c from tableName 那么输出就是 abc对应的顺序

㈨ SQL 语句列转行

TB_NAME_A Count(TC_TA_ID=TA_ID && TC_TB_ID=TB_ID_A)
TB_NAME_B Count(TC_TA_ID=TA_ID && TC_TB_ID=TB_ID_B)

TC_TB_ID=TB_ID_A,TC_TB_ID=TB_ID_B 这个TB_ID_A,TB_ID_B 代表什么?

㈩ sql 最简单的列转行

oracle中列传行可用wm_concat来实现。
如test表中数据如下:
现要将name列一列显示成行,可用如下语句:
select wm_concat(name) from test;结果: