createtable
student
( id
intprimary
keyidentity
, km
varchar(50
), name
varchar(52
), score
int)
select
*from
student
列轉行/*
select [姓名],
max(case 課程 when '語文' then 分數 else 0 end) as 語文,
max(case 課程 when '數學' then 分數 else 0 end)as 數學,
max(case 課程 when '物理' then 分數 else 0 end)as 英語,
from student group by [姓名]
*/select
name,
max(case km when'語文
'then score else
0end) as
語文,
max(case km when'數學
'then score else
0end)as
數學,
max(case km when'物理
'then score else
0end)as
英語from student group
byname
--使用pivot列轉行
select
*from(
select
score ,
km ,
name
from
student
)student pivot(
max(score) for km in
(語文,數學,英語)) t
create
table
studenttwo
( id
intprimary
keyidentity
, name
varchar(52
), yuwen
int,
shuxue
int,
yingyu
int,
)select
*from
studenttwo
--列轉行
select
*from
(
select name as'姓名
', '語文'
as'科目', yuwen as'成績
'from
studenttwo
union
allselect name as'姓名
', '數學'
as'科目', shuxue as'成績
'from
studenttwo
union
allselect name as'姓名
', '英語'
as'科目', yingyu as'成績
'from
studenttwo
)tselect
*from(
select name as'姓名
', '語文'
as'科目' ,yuwen as'成績
'from
studenttwo
)t--使用unpivot列轉行
select
*from studenttwo unpivot(成績 for 科目 in(yuwen,shuxue,yingyu)) t
行轉列 列轉行
行轉列 select t.t.rowid from test1 t id c1 c2 c3 1 小紅 數學 10 2 小紅 語文 20 3 小欄 數學 15 4 小欄 語文 25 test1 oracle select c1,to char wm concat c2 c2 from test1 gr...
hive 列轉行 HQL 行轉列,列轉行
1 相關函式 concat string a col,string b col 返回輸入字串連線後的結果,支援任意個輸入字串 concat ws separator,str1,str2,它是乙個特殊形式的 concat 第乙個引數剩餘引數間的分隔符。分隔符可以是與剩餘引數一樣的字串。如果分隔符是 n...
python 列轉行 SQL 行轉列,列轉行
sql 行轉列,列轉行 行列轉換在做報表分析時還是經常會遇到的,今天就說一下如何實現行列轉換吧。行列轉換就是如下圖所示兩種展示形式的互相轉換 行轉列假如我們有下表 select from student pivot sum score for subject in 語文,數學,英語 通過上面 sql...