Skip to content

Latest commit

 

History

History
71 lines (44 loc) · 2.8 KB

20240503_04.md

File metadata and controls

71 lines (44 loc) · 2.8 KB

DuckDB unpivot 行列转换 internals

作者

digoal

日期

2024-05-03

标签

PostgreSQL , PolarDB , DuckDB , unpivot , 行列转换 , 统计 , internals


背景

unpivot 用法参考如下, 本文主要演示DuckDB内部如何转换unpivot结果? 帮助更好的理解unpivot.

手册:

更多相关参考:

unpivot 语法 sql:

UNPIVOT monthly_sales  
ON jan, feb, mar, apr, may, jun    
INTO  
    NAME month   
    VALUE sales;   

自动转换为如下sql:

SELECT  
    empid,  
    dept,  
    unnest(['jan', 'feb', 'mar', 'apr', 'may', 'jun']) AS month,  -- 单引号表示 取 单引号内字符串 的值  
    unnest(["jan", "feb", "mar", "apr", "may", "jun"]) AS sales  -- 双引号表示 取 双引号内字段名 对应的值  
FROM monthly_sales;  

digoal's wechat