當前位置:首頁 » 編程語言 » sql列變行怎麼變
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql列變行怎麼變

發布時間: 2022-05-05 07:33:51

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到其它中就行了。
縱橫轉換就是把縱向的有限名稱值轉為列名:這就是轉置的本質。