在Netezza中,GROUP_CONCAT函数不支持ORDER BY,它使用自己的内部顺序,它是用作参数的列的顺序。
SELECT 'GROUP BY '||TOOLKIT.SQLEXT.GROUP_CONCAT(PRIMARY_KEY, ', ') AS GROUP_BY
FROM (
SELECT 1 AS SEQ, 'DATA_DATE' AS PRIMARY_KEY
UNION ALL
SELECT 2 AS SEQ, 'ACCT_ID' AS PRIMARY_KEY
) S;
这将返回:
GROUP_BY GROUP BY ACCT_ID, DATA_DATE
但是如何通过SEQ而不是PRIMARY_KEY获得结果顺序来得到这样的结果:
GROUP_BY GROUP BY DATA_DATE, ACCT_ID
答案 0 :(得分:0)
我们可以在第一个GROUP_CONCAT参数的开头添加SEQ以强制内部ORDER BY为我们工作,然后删除我们添加的内容。为了安全删除,我们在序列的开头和结尾添加了额外的“@”。
SELECT 'GROUP BY '||TOOLKIT.SQLEXT.REGEXP_REPLACE(
TOOLKIT.SQLEXT.GROUP_CONCAT('@'||LPAD(SEQ, 4, '0')||'@'
||PRIMARY_KEY, ', '),
'@[0-9]{4}@', '') AS GROUP_BY
FROM (
SELECT 1 AS SEQ, 'DATA_DATE' AS PRIMARY_KEY
UNION ALL
SELECT 2 AS SEQ, 'ACCT_ID' AS PRIMARY_KEY
) S;
这将返回我们想要的内容:
GROUP_BY GROUP BY DATA_DATE, ACCT_ID