[英]Big Query: Query doesn't allow us to get the min of a struct
I recently moved from using apache hive to GCP big query as my SQL database.我最近从使用 apache hive 转移到 GCP 大查询作为我的 SQL 数据库。
I have a set of data as follows,我有一组数据如下,
ID ![]() |
GROUP![]() |
GROUP_TYPE ![]() |
OFFSET![]() |
OFFSET_IDENTIFIER ![]() |
---|---|---|---|---|
1 ![]() |
100 ![]() |
A![]() |
120 ![]() |
1 ![]() |
2 ![]() |
100 ![]() |
A![]() |
140 ![]() |
2 ![]() |
3 ![]() |
500 ![]() |
B![]() |
120 ![]() |
1 ![]() |
4 ![]() |
500 ![]() |
B![]() |
140 ![]() |
2 ![]() |
5 ![]() |
5000 ![]() |
C ![]() |
300 ![]() |
1 ![]() |
6 ![]() |
5000 ![]() |
C ![]() |
250 ![]() |
2 ![]() |
7 ![]() |
10000 ![]() |
D ![]() |
24 ![]() |
1 ![]() |
8 ![]() |
10000 ![]() |
D ![]() |
46 ![]() |
2 ![]() |
9 ![]() |
10000 ![]() |
D ![]() |
99 ![]() |
3 ![]() |
10 ![]() |
10000 ![]() |
D ![]() |
11 ![]() |
4 ![]() |
In hive I used the following query to get the min of offset by group_type and returned the min offset and corresponding offset identifier,在 hive 中,我使用以下查询通过 group_type 获取偏移量的最小值并返回最小偏移量和相应的偏移量标识符,
select
ID,
GROUP,
GROUP_TYPE,
min(struct(OFFSET,OFFSET_IDENTIFIER)).col1 as min_offset,
min(struct(OFFSET,OFFSET_IDENTIFIER)).col2 as offset_identifier from eng.offsets
group by ID,GROUP,GROUP_TYPE;
The output I get from this query is as follows,我从这个查询中得到的 output 如下,
ID ![]() |
GROUP![]() |
GROUP_TYPE ![]() |
OFFSET![]() |
OFFSET_IDENTIFIER ![]() |
---|---|---|---|---|
1 ![]() |
100 ![]() |
A![]() |
120 ![]() |
1 ![]() |
3 ![]() |
500 ![]() |
B![]() |
120 ![]() |
1 ![]() |
6 ![]() |
5000 ![]() |
C ![]() |
250 ![]() |
2 ![]() |
10 ![]() |
10000 ![]() |
D ![]() |
11 ![]() |
4 ![]() |
But when I execute the same query in BQ,但是当我在 BQ 中执行相同的查询时,
select
ID,
GROUP,
GROUP_TYPE,
min(struct(OFFSET,OFFSET_IDENTIFIER)).OFFSET as min_offset,
min(struct(OFFSET,OFFSET_IDENTIFIER)).OFFSET_IDENTIFIER as offset_identifier
from eng.offsets
group by ID,GROUP,GROUP_TYPE;
I get the following error: MIN is not defined for arguments of type STRUCT
我收到以下错误:
MIN is not defined for arguments of type STRUCT
May I check if there is any solution to this issue/a way to get the min of struct?我可以检查是否有解决此问题的方法/获取结构最小值的方法吗?
Thank you for your inputs!感谢您的投入!
Consider query below:考虑以下查询:
with sample_data as (
select 1 as id, 100 as groupp, 'A' as group_type, 120 as offsett, 1 as offset_identifier
union all select 2 as id, 100 as groupp, 'A' as group_type, 140 as offsett, 2 as offset_identifier
union all select 3 as id, 500 as groupp, 'B' as group_type, 120 as offsett, 1 as offset_identifier
union all select 4 as id, 500 as groupp, 'B' as group_type, 140 as offsett, 2 as offset_identifier
union all select 5 as id, 5000 as groupp, 'C' as group_type, 300 as offsett, 1 as offset_identifier
union all select 6 as id, 5000 as groupp, 'C' as group_type, 250 as offsett, 2 as offset_identifier
union all select 7 as id, 10000 as groupp, 'D' as group_type, 24 as offsett, 1 as offset_identifier
union all select 8 as id, 10000 as groupp, 'D' as group_type, 46 as offsett, 2 as offset_identifier
union all select 9 as id, 10000 as groupp, 'D' as group_type, 99 as offsett, 3 as offset_identifier
union all select 10 as id, 10000 as groupp, 'D' as group_type, 11 as offsett, 4 as offset_identifier
),
add_min_offset as(
select
id,
groupp,
group_type,
offsett,
min(offsett) over (partition by group_type) as min_offset,
offset_identifier
from sample_data
)
select * except(offsett) from add_min_offset
where offsett=min_offset
Output: Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.