码迷,mamicode.com
首页 > 数据库 > 详细

vertica sql实现行转列

时间:2020-02-10 13:21:33      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:code   union   create   number   gre   table   group   rom   nvl   

CREATE TABLE t1 (
  id int,
  name varchar(10)
);

INSERT INTO t1
SELECT 1 AS id, greg AS name
UNION ALL
SELECT 2, paul
UNION ALL
SELECT 3, greg
UNION ALL
SELECT 4, greg
UNION ALL
SELECT 5, paul;

COMMIT;

SELECT name,
    MAX(DECODE(row_number, 1, a.id)) ||
    NVL(MAX(DECODE(row_number, 2, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 3, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 4, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 5, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 6, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 7, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 8, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 9, , || a.id)), ‘‘) ||
    NVL(MAX(DECODE(row_number, 10, , || a.id)), ‘‘) id
FROM
    (SELECT name, id, ROW_NUMBER() OVER(PARTITION BY name ORDER BY id) row_number FROM t1) a
GROUP BY a.name
ORDER BY a.name;

Result

 name |  id
------+-------
 greg | 1,3,4
 paul | 2,5

vertica sql实现行转列

标签:code   union   create   number   gre   table   group   rom   nvl   

原文地址:https://www.cnblogs.com/lavezhang/p/12290537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!