gpt4 book ai didi

google-bigquery - GROUP BY 和 HAVING with ALIAS 的 Bigquery 错误

转载 作者:行者123 更新时间:2023-12-01 05:53:37 34 4
gpt4 key购买 nike

我注意到 bigquery 标准 sql 中有一个奇怪的错误。
我有一张 table :

SELECT * FROM ds.sod;
| id | name |
|----+-------|
| 1 | tom |
| 2 | dick |
| 3 | harry |

因此,如果我按 exporession 分组它会起作用
SELECT MOD(id,2) AS oddeven, COUNT(1) AS cnt
FROM ds.sod GROUP BY MOD(id,2)
| oddeven | cnt |
+---------+-----+
| 1 | 2 |
| 0 | 1 |

但是如果我添加一个 HAVING 子句,它就会失败。
SELECT MOD(id,2) AS oddeven, COUNT(1) AS cnt
FROM ds.sod GROUP BY MOD(id,2) HAVING COUNT(1) > 0
ERROR: SELECT list expression references column id which is neither grouped nor aggregated

现在奇怪的是,如果我不为列设置别名
SELECT MOD(id,2), COUNT(1) AS cnt
FROM ds.sod GROUP BY MOD(id,2) HAVING COUNT(1) > 0
| f0_ | cnt |
+-----+-----+
| 1 | 2 |
| 0 | 1 |

如果我不使用函数,它也适用于别名
SELECT id AS oddeven, COUNT(1) AS cnt
FROM ds.sod GROUP BY id HAVING COUNT(1) > 1
| oddeven | cnt |
+---------+-----+
| 3 | 1 |
| 2 | 1 |
| 1 | 1 |

难道我做错了什么?或者这是 bigquery 标准 SQL 解析中的错误?

编辑:刚刚注意到如果我按别名分组它确实有效(我从未做过的事情因为在 oracle 7 中不起作用)
SELECT MOD(id,2) AS oddeven, COUNT(1) AS cnt
FROM ds.sod GROUP BY oddeven HAVING COUNT(1) > 0
| oddeven | cnt |
+---------+-----+
| 1 | 2 |
| 0 | 1 |

最佳答案

或者您可以使用列位置

with

sample_data as (
select
*
from
unnest(
array[
struct(1 as id, 'tom' as name),
struct(2, 'dick'),
struct(3, 'harry')
]
)
)

select
mod(id, 2) as oddeven,
count(*) as cnt
from
sample_data
group by
1
having
count(*) > 0

关于google-bigquery - GROUP BY 和 HAVING with ALIAS 的 Bigquery 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51438427/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com