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>)
)