Presto行转列 列转行

hive中表结构如下:

 CREATE TABLE hive.default.t1 ( 
    day integer,                
    days integer                
 )  

查看数据

presto:default> select * from t1;
 day | days 
-----+------
   1 |    1 
   3 |    3 
   2 |    2 
(3 rows)

列转行

presto:default> select array_agg(day) d1,array_agg(days) d2 from (select day,days from t1 order by day asc,days asc)
-> ;
d1 | d2
-----------±----------
[1, 2, 3] | [1, 2, 3]
(1 row)
至于上面那个查询为什么会加个子查询,里面对day,days排序,大家可以试试

select array_agg(day) d1,array_agg(days) d2 from t1; 多运行几次,看看结果,可以看到[1,2,3]每次出来的结果不一致,无序的

行转列(这里就不做新的表了,利用上面的输出结果当做数据来源,因为他保存也是个行的数据)

介绍CROSS JOIN UNNEST的用法

官网举例:
SELECT student, score
FROM tests
CROSS JOIN UNNEST(scores) AS t (score);
**Demo**:
SELECT numbers, animals, n, a
FROM (
  VALUES
    (ARRAY[2, 5], ARRAY['dog', 'cat', 'bird']),
    (ARRAY[7, 8, 9], ARRAY['cow', 'pig'])
) AS x (numbers, animals)
CROSS JOIN UNNEST(numbers, animals) AS t (n, a);
  numbers  |     animals      |  n   |  a
-----------+------------------+------+------
 [2, 5]    | [dog, cat, bird] |    2 | dog
 [2, 5]    | [dog, cat, bird] |    5 | cat
 [2, 5]    | [dog, cat, bird] | NULL | bird
 [7, 8, 9] | [cow, pig]       |    7 | cow
 [7, 8, 9] | [cow, pig]       |    8 | pig
 [7, 8, 9] | [cow, pig]       |    9 | NULL
(6 rows)

下面的dd1,dd2,ddd1纯属于个人随便起的别名

presto:default> select dd1,dd2,ddd1,ddd2
             -> from(
             -> select array_agg(day) d1,array_agg(days) d2 from (select day,days from t1 order by day asc,days asc)
             -> ) as x (dd1,dd2)
             -> CROSS JOIN UNNEST(dd1,dd2) as t (ddd1,ddd2)
             -> ;
    dd1    |    dd2    | ddd1 | ddd2 
-----------+-----------+------+------
 [1, 2, 3] | [1, 2, 3] |    1 |    1 
 [1, 2, 3] | [1, 2, 3] |    2 |    2 
 [1, 2, 3] | [1, 2, 3] |    3 |    3 
(3 rows)

Query 20190617_060635_00354_2rb8i, FINISHED, 4 nodes
Splits: 72 total, 72 done (100.00%)
0:00 [3 rows, 12B] [8 rows/s, 35B/s]

presto:default> select ddd1,ddd2
             -> from(
             -> select array_agg(day) d1,array_agg(days) d2 from (select day,days from t1 order by day asc,days asc)
             -> ) as x (dd1,dd2)
             -> CROSS JOIN UNNEST(dd1,dd2) as t (ddd1,ddd2)
             -> ;
 ddd1 | ddd2 
------+------
    1 |    1 
    2 |    2 
    3 |    3 
(3 rows)

行转列如果上面的2个看不懂的话,可以只查询一个字段

presto:default> SELECT  days
             -> FROM (
             ->     select array_agg(day) d1 from (select day,days from t1 order by day asc,days asc)
             -> ) AS x (dd1)
             -> CROSS JOIN UNNEST(dd1) AS t (days);
 days 
------
    1 
    2 
    3 
(3 rows)


你可能感兴趣的:(bigdata)