我在select
语句中有很多列,其中许多是派生计算。
我正在尝试在listagg()
语句中使用select
将多行分组为一个行,而不必在group by
语句中select
其余列进行分组。遵循listagg() within group() over (partition by id)
的思路。
现在我有一些类似的东西:
select id, listagg(distinct annual_bill_rate, ', ') within group (order by bill_rate) as annual_bill_rate, email, state
from table
group by 1,3,4
根据文档,似乎不可能避免此问题,但是有替代方法吗?我有30多个专栏,无法按所有人进行分组。谢谢!
样本数据:
id bill_rate email state
1 0.0035 a@gmail.com NJ
1 0.0045 a@gmail.com NJ
1 0.0055 a@gmail.com NJ
2 0.0065 b@gmail.com NY
2 0.0075 b@gmail.com NY
3 0.0085 c@gmail.com PA
所需结果-无分组依据:
id bill_rate email state
1 0.0035, 0.0045, 0.0055 a@gmail.com NJ
2 0.0065, 0.0075 b@gmail.com NY
3 0.0085 c@gmail.com PA
答案 0 :(得分:1)
这里最好避免输入GROUP BY。几乎肯定会比较慢,而且阅读和理解也要困难得多。如果在生产代码中遇到这个问题,我将是一个不高兴的家伙:
WITH table_distinct AS
(
SELECT DISTINCT id, email, state
FROM table
)
,table_group_by AS
(
SELECT id, listagg(distinct annual_bill_rate, ', ') within group (order by bill_rate) as annual_bill_rate
FROM table
GROUP BY id
)
SELECT
td.*,
tgb.annual_bill_rate
FROM table_distinct td
INNER JOIN table_group_by tgb
ON td.id = tgb.id;
现在,您实际上只需要具有table_distinct
CTE的猴子即可向结果集中添加更多列。