1. 行转列,列转行怎么做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
2. sql 最简单的列转行
set nocount on ;
declare @T table(ID int)
insert @T select 1
insert @T select 2
insert @T select 3
insert @T select 4
insert @T select 5
insert @T select 6
insert @T select 7
insert @T select 8
insert @T select 9
insert @T select 0
;with c
as
(
select
row=(row_number()over(order by (select 1))-1)/5,* from @T)
select
ID=(select rtrim(ID) from c where row=a.row for xml path(''))
from C a
group by row
ID
----------------------
12345
67890
3. 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
4. sql把列变成行
可以用pivot table
5. SQL 行列互换(有多列数据,只需其中的两列转成行),求大神指导一下
这个问题用Excel做来秒出,如果数据量不是很大可以考虑用excel来做。
6. SQL语句如何列变行
select 字段名按照你要求的排序就行了
select a,b,c from tableName 那么输出就是 abc对应的顺序
7. 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 合计=废塑料+废五金+废钢铁+废纸+废有色+废纤维+其它;
(所有涉及表的行列转换均可按照这种方式实现。)
8. 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'
--你试试
9. sql 列转行
selectcasewhentab1.tutorldisnullthentab2.tutorldelsetab1.tutorldend,tab1.菜品1,tab2.菜品2from
(selecttutorld,dishnameas'菜品1'from表格名称wheredishtype=1)tab1
fulljoin
(selecttutorld,dishnameas'菜品2'from表格名称wheredishtype=2)tab2
ontab1.tutorld=tab2.tutorld
;
10. 如何将列变成行 sql server
可以使用以下SQL语句取出来:
select hsname from sum1 group by hsname
把需要的都建上,不需要的sum到其它中就行了。
纵横转换就是把纵向的有限名称值转为列名:这就是转置的本质。