如何透视表并连接列

时间:2019-06-14 19:52:34

标签: sql oracle

我正在使用此SQL查询。它旨在获取表名称及其关联的主键:

SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cons.constraint_type = 'P'
--AND cols.table_name = 'TABLE_NAME'
AND cons.OWNER = 'MYOWNER'
AND cols.COLUMN_NAME <> 'LINE'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;

这将返回

TABLE_NAME| COLUMN_NAME| POSITION| STATUS| OWNER
================================================
TABLE1    | COL1       | 1       | ENABLED| USER1
TABLE1    | COL2       | 2       | ENABLED| USER1
TABLE1    | COL3       | 3       | ENABLED| USER1
TABLE2    | COL1       | 1       | ENABLED| USER1
TABLE3    | COL1       | 1       | ENABLED| USER1

我想要实现的是:

TABLE_NAME| COLUMNS          | OWNER
TABLE1    | COL1, COL2, COL3 | USER1  
TABLE2    | COL1             | USER1
TABLE3    | COL1             | USER1

如何将这些列混在一起? 谢谢!

0 个答案:

没有答案