MySQL之進行行列轉換

2021-08-10 02:29:04 字數 4316 閱讀 4855

內容介紹:

如何進行行列轉換

如何生成唯一序列號

如何刪除重複資料

一:如何進行行列轉換

需要用到的場景:主要兩個場景(報表統計+彙總顯示)

使用自連線實現行列轉換

行轉列比如成績

1:分別查詢出不同同學的成績,並將欄位名改為同學的名字。

2:通過交叉連線,將不同的語句連線起來

select *

from (

select

sum(kills) as

'a'from a inner

join b on a.name=b.user_name

where a.name='a') as a cross

join(

select

sum(kills) as

'b'from a inner

join b on a.name=b.user_name

where a.name='b') as b cross

join(

select

sum(kills) as

'c'from a inner

join b on a.name=b.user_name

where a.name='c')as c;

案例:

select * from (

select

sum(kills) as

'孫悟空'

from user1 a

join user_kills b

on a.id = b.use_id and a.user_name='孫悟空'

) a cross

join (

select

sum(kills) as

'豬八戒'

from user1 a

join user_kills b

on a.id = b.use_id and a.user_name='豬八戒'

) b cross

join(

select

sum(kills) as

'沙僧'

from user1 a

join user_kills b

on a.id = b.use_id and a.user_name='沙僧'

) c

注意:其中括號後面的a b c是給select語句結果表的命名

select

sum(kills) as

'孫悟空'

from user1 a

join user_kills b

on a.id = b.use_id and a.user_name='孫悟空'

查詢出悟空的打怪數量,沙僧豬八戒同理。

缺點:是將原來查詢的結果每一行單獨查詢出來,再進行拼接,因此每增加乙個同學就增加乙個select語句,並且是通過交叉連線,要保證每個查詢的結果只能是乙個,不然沒辦法通過交叉連線實現轉換。

使用case實現行列轉換

select

sum(case

when user_name='孫悟空'

then kills end) as

'孫悟空',

sum(case

when user_name='豬八戒',thwn kills end) as

'豬八戒')

sum(case

when user_name='沙僧'

then kills end) as

'沙僧'

from user_name a join user_kills b on a.id = b.user_id;

使用case語句

select

sum(case user_name='a'

then kills end) as

'a',

sum(case username='b'

then kill end) as

'b',

sum(case username='c'

then kill end) as

'c'from a inner

join b on a.name=b.username;

通過case給滿足when語句的資料返回sum聚合函式要統計的資料,再將sum進行重新命名。推薦。

一般形式

select name as

'姓名',

sum(case subject when

'數學'

then mark else

null

end) as

'數學',

sum(case subject when

'英語'

then mark else

null

end) as

'英語',

sum(case subject when

'語文'

then mark else

null

end) as

'語文'

from subject

group

by name;

case語句中放原來表中要進行轉換的字段(如學科),對其進行分類(如數學、英語、語文)。

注意:要用聚合函式,最後要group by。

列轉行的實現

單列轉多行:屬性拆分,etl資料處理

利用序列表處理列轉行的資料

select user_name,replace(substring(substring_index(mobile,',',a_id),char_length(

substring_index(mobile,',',a.id-1),',',")as mobile from tb_sequernce a

cross join select user_name,

concat(mobile,',') as mobile,length(mobile)-length(replace(mobile,',',"))+1

size

from user1 b ) b on a.id<=b.size;

序列表:存在序列號的表。tb_sequence是序列表。

直接替換user1

使用union all將結果集合並

select user_name,'arms'

as equipment, arms from user1 a join user1_equipment b on a.id=user_id

union

allselect user_name,'clothing'

as equipment, clothing from user1 a join user1_equipment b on a.id=user_id;

union all

select user_name,'shoe'

as equipment, shoe from user1 a join user1_equipment b on a.id=user_id;

使用序列化的方式列轉行

select username,

(case

when s.id=1

then

'arms'

when s.id=2

then

'clothing'

when s.id=3

then

'shoe'

end) equipment,

(case

when s.id=1

then arms

when s.id=2

then clothing

when s.id=3

then shoe end) eq_name

from t_equipment e join t_user u

on e.userid = u.userid

cross

join t_sequence s where s.id<=3

order

by username

SQL進行行列轉換

假設現在有這樣一張表 create table dbo relconlist listid allint identity 1,1 not null listfkmainid varchar 20 collate chinese prc ci as not null listfkrelid varc...

sql進行行列轉換

1.分別求出孫悟空 豬八戒 沙僧的總打怪數 select sum kills from user1 a join user kills b on a.id b.user id where a.user name 孫悟空 select sum kills from user1 a join user ...

使用 case when進行行列轉換

固定列數的行列轉換,表結構為 轉換後 要求 建立表,源表,表名 student 只用一句sql 得到轉換結果。解答 方法一 通過生成臨時表的方式操作 select name sum yw as 語文 sum sx as 數學 sum wy as 英語 from select name case su...