偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

SQL實用技巧-行列轉(zhuǎn)換

數(shù)據(jù)庫 SQL Server
PIVOT關(guān)鍵字對于指定的每一組行值,都會生成對應(yīng)的列。PIVOT關(guān)鍵字是FROM子句的一部分,可以和JOIN等其他關(guān)鍵字一同使用

在編寫大數(shù)據(jù)SQL的時候,有時需要進(jìn)行行列的轉(zhuǎn)化。

什么是行列轉(zhuǎn)化?如下圖,不同商品在不同月份的銷量數(shù)據(jù),有時候我們希望數(shù)據(jù)和左側(cè)一樣的排列,但原始數(shù)據(jù)卻像右側(cè)一樣排列,此時我們需要把右側(cè)的列排列轉(zhuǎn)換成左側(cè)的行排列,反之亦然。

行轉(zhuǎn)列與列轉(zhuǎn)行行轉(zhuǎn)列與列轉(zhuǎn)行

下面以上面這個例子為大家介紹一些行列轉(zhuǎn)換的方式。

行轉(zhuǎn)列

使用CASE WHEN

適用場景:MySQL、Hive、Spark SQL。

把行轉(zhuǎn)換成列最簡單的方式就是使用CASE WHEN。

case month when '2024-01' then sales end的意思是當(dāng)month的值為'2024-01'時取sales的值,其他情況取NULL,因此可以計算出不同月份的銷量。

select  product
        ,max(case month when '2024-01' then sales end) as month_01
        ,max(case month when '2024-02' then sales end) as month_02
        ,max(case month when '2024-03' then sales end) as month_03
from    sales_row
group by product

使用PIVOT

適用場景:Spark SQL。

PIVOT關(guān)鍵字對于指定的每一組行值,都會生成對應(yīng)的列。PIVOT關(guān)鍵字是FROM子句的一部分,可以和JOIN等其他關(guān)鍵字一同使用。

SELECT ... 
FROM ... 
PIVOT ( 
    <aggregate function> [AS <alias>] [, <aggregate function> [AS <alias>]] ... 
    FOR (<column> [, <column>] ...) 
    IN ( 
        (<value> [, <value>] ...) AS <new column> 
        [, (<value> [, <value>] ...) AS <new column>] 
        ... 
       ) 
    ) 
[...]

參數(shù)

是否必選

說明

aggregate function


聚合函數(shù)

alias


聚合函數(shù)的別名,別名和最終PIVOT處理過后生成的列名相關(guān)

column


指定轉(zhuǎn)換為列的行值在源表中的列名稱

value


指定轉(zhuǎn)換為列的行值

new column


轉(zhuǎn)換后新的列名稱

直接看示例。

利用PIVOT把month列按值聚合出了三列month_01,month_02,month_03。

select  *
from    sales_row 
PIVOT (
     MAX(sales) for month in(
       '2024-01' as month_01, 
       '2024-02' as month_02, 
       '2024-03' as month_03
     )
)

列轉(zhuǎn)行

使用UNION ALL

適用場景:MySQL、Hive、Spark SQL。

UNION ALL相當(dāng)于取每一個列的值,然后并聯(lián)在一起,注意'2024-01' as month中的2024-01是字符串。

使用UNION ALL的好處就是,無論是mysql、hive還是spark都支持,以不變應(yīng)萬變。

缺點就是當(dāng)要關(guān)聯(lián)列比較多時比較麻煩,如果要查詢?nèi)甑臄?shù)據(jù),則需要UNION ALL 12次,如果是天數(shù)據(jù)則要UNION ALL 365次。

select  *
from    (
    select product, '2024-01' as month, month_01 from sales_column
    union all
    select product, '2024-02' as month, month_02 from sales_column
    union all
    select product, '2024-03' as month, month_03 from sales_column
)

僅使用EXPLODE

適用場景:Spark SQL。

explode可以將一個數(shù)組或者map分解成多行,例如:

select explode(split('A,B,C', ','))

# 結(jié)果
col
A
B
C
select explode(map('2024-01', 1000, '2024-02', 2000, '2024-03', 3000))

# 結(jié)果
key     value
2024-01 1000
2024-02 2000
2024-03 3000

對于列轉(zhuǎn)行的需求,可以先創(chuàng)建一個map之后再利用explode拆分成多行。

注意下面SQL中,explode函數(shù)返回值有兩個,因此設(shè)置列別名時需要用as (month, sales)。

select  product
        ,explode(
          map('2024-01', month_01, 
              '2024-02', month_02, 
              '2024-03', month_03)
        ) as (month, sales)
from    sales_column

類似的思路還可以利用concat+trans_array等操作。

hive中的UDTF

上面的方式僅適用于Spark。

當(dāng)使用UDTF函數(shù)(explode就是一個UDTF函數(shù))的時候,Hive只允許對拆分字段進(jìn)行訪問。

select explode(map('2024-01', 1000, '2024-02', 2000, '2024-03', 3000))

# 結(jié)果
key     value
2024-01 1000
2024-02 2000
2024-03 3000

也就是說在Hive中,上面SQL是沒問題的,下面的SQL就會報錯了

hive> select  product
    >   ,explode(map('2024-01', month_01, '2024-02', month_02, '2024-03', month_03))
  >  from    sales_column

SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions

因此這塊需要使用LATERAL VIEW功能來進(jìn)行處理。LATERAL VIEW將explode生成的結(jié)果當(dāng)做一個視圖來處理。

使用Lateral View

適用場景:Hive、Spark SQL。

lateral view為側(cè)視圖,意義是為了配合UDTF來使用,把某一行數(shù)據(jù)拆分成多行數(shù)據(jù)。

Hive中不加lateral view的UDTF只能提取單個字段拆分。加上lateral view就可以將拆分的單個字段數(shù)據(jù)與原始表數(shù)據(jù)關(guān)聯(lián)上。

LATERAL VIEW [ OUTER ] generator_function ( expression [ , ... ] ) [ table_alias ] AS column_alias [ , ... ]

參數(shù)

是否必選

說明

generator_function


將一行數(shù)據(jù)拆成多行數(shù)據(jù)的UDTF (EXPLODE, INLINE等)

table_alias


UDTF結(jié)果的別名

columnAlias


拆分后得到的列的別名

直接看如何利用lateral view實現(xiàn)列轉(zhuǎn)行。

select  product, t_view.month, t_view.sales
from    sales_column
lateral view explode(
    map('2024-01', month_01, '2024-02', month_02, '2024-03', month_03)
) t_view as month, sales

其中explode(map('2024-01', month_01, '2024-02', month_02, '2024-03', month_03))把map分解成多行。

lateral view同時指定了這個側(cè)視的表名t_view和兩列的列名month 、sales。

lateral view explode(
    map('2024-01', month_01, '2024-02', month_02, '2024-03', month_03)
) t_view as month, sales

# 模擬結(jié)果,lateral view不能單獨使用
month sales
2024-01 1000
2024-02 1100
2024-03 1200
2024-01 1100
2024-02 1000
2024-03 1400

此時select product, t_view.month, t_view.sales就能達(dá)成UDTF拆分的單個字段數(shù)據(jù)與原始表數(shù)據(jù)關(guān)聯(lián)的效果了。

select  product, t_view.month, t_view.sales
from    sales_column

# 結(jié)果
product month sales
A 2024-01 1000
A 2024-02 1100
A 2024-03 1200
B 2024-01 1100
B 2024-02 1000
B 2024-03 1400

使用UNPIVOT

適用場景:Spark 3.4+。

UNPIVOT關(guān)鍵字對于指定的每一組列,都會生成對應(yīng)的行。其中UNPIVOT關(guān)鍵字是FROM子句的一部分,可以和JOIN關(guān)鍵字等其他關(guān)鍵字一同使用。

SELECT ...
FROM ...
UNPIVOT (
  <new column of value> [, <new column of value>] ...
  FOR (<new column of name> [, <new column of name>] ...)
  IN (
      (<column> [, <column>] ...) [AS (<column value> [, <column value>] ...)]
      [, (<column> [, <column>] ...) [AS (<column value> [, <column value>] ...)]]
      ...
    )
)
[...]

參數(shù)說明如下:

參數(shù)

是否必選

說明

new column of value


轉(zhuǎn)換后新生成的列名稱,該列的值由指定轉(zhuǎn)換為行的列的值填充。

new column of name


轉(zhuǎn)換后新生成的列名稱,該列的值由指定轉(zhuǎn)換為行的列名稱填充。

column


指定轉(zhuǎn)換為行的列名稱,列的名稱用來填充new column of name;列的值用來填充new column of value。

column value


指定轉(zhuǎn)換為行的列的別名

也是直接看示例:

select  *
from    sales_column 
UNPIVOT (
  sales for month in (month_01 as '2024-01', month_02 as '2024-02', month_03 as '2024-03')
)

sales for month in (month_01, month_02, month_03)的意思就是生成一個新列sales,這一列的值是month_01, month_02, month_03這三列的值。

生成一個新列month, 這里一列的值是month_01, month_02, month_03這三列的列名,即'2024-01',  '2024-02', '2024-03'。

責(zé)任編輯:武曉燕 來源: 涼涼的知識庫
相關(guān)推薦

2022-03-23 09:18:10

Git技巧Linux

2009-09-04 10:27:28

Linux實用技巧linux操作系統(tǒng)linux

2009-12-21 15:50:39

2022-11-03 10:28:59

PandasSAC機(jī)制

2011-04-08 15:40:01

Oracle認(rèn)證

2022-10-11 08:00:47

多線程開發(fā)技巧

2009-01-03 09:34:30

ASP.NET.NET性能優(yōu)化

2009-12-09 11:21:30

Linux實用技巧

2010-09-14 10:41:24

DIV+CSS排版

2010-10-08 15:44:17

vim

2019-11-25 10:12:59

Python技巧工具

2019-12-22 23:10:19

LinuxSSH加密

2009-12-23 17:32:35

Linux構(gòu)建軟路由

2019-10-10 16:31:51

PyCharmPythonWindows

2022-05-30 09:01:13

CSS技巧前端

2019-10-12 15:42:36

CSS代碼前端

2010-11-02 15:36:30

jQuery

2022-09-15 07:05:09

Windows電腦技巧

2011-03-23 16:49:17

LAMP技巧linux命令

2010-01-14 10:18:24

VB.NET List
點贊
收藏

51CTO技術(shù)棧公眾號