gpt4 book ai didi

Group row values by the sequence of their appearance(按行值的出现顺序对行值进行分组)

转载 作者:bug小助手 更新时间:2023-10-28 22:36:45 25 4
gpt4 key购买 nike



In BigQuery I have a table like this:

在BigQuery中,我有一个如下所示的表:
















































Order Value
1 A
2 A
3 A
4 B
5 B
7 A
8 A
10 A
11 C


The order column is increasing but not continuously in every case. The Value column can have returning values. I need to create groups for the Values by the combination of the value and the number of appearances of the value.
So basically I need a new column (Group) like this:

订单栏在增加,但不是在每种情况下都是连续的。“值”列可以具有返回值。我需要通过值和值的出现次数的组合来为值创建组。所以基本上我需要一个这样的新列(组):


























































Order Value Group
1 A 1
2 A 1
3 A 1
4 B 2
5 B 2
7 A 3
8 A 3
10 A 3
11 C 4


I tried to use dense_rank() in different variations, this is the best that I've got, but that's not what I need.

我尝试在不同的变体中使用Dense_Rank(),这是我得到的最好的结果,但这不是我所需要的。




































































Order Value dense_rank() over(order by Value) Right solution
1 A 1 1
2 A 1 1
3 A 1 1
4 B 2 2
5 B 2 2
7 A 1 3
8 A 1 3
10 A 1 3
11 C 4 4


Code for the test data:

测试数据代码:


with test_data as(
select 1 as _order, 'A' as value
union all
select 2 as _order, 'A' as value
union all
select 3 as _order, 'A' as value
union all
select 4 as _order, 'B' as value
union all
select 5 as _order, 'B' as value
union all
select 7 as _order, 'A' as value
union all
select 8 as _order, 'A' as value
union all
select 10 as _order, 'A' as value
union all
select 11 as _order, 'C' as value
)
select _order,
value,
dense_rank() over(order by value) value_order,
from test_data
order by _order

更多回答
优秀答案推荐

You could address this problem with a running count over a computed flag, that has non-null value (1) when there's a change between current "Value" and previous "Value".

您可以通过计算标志的运行计数来解决这个问题,当当前“值”和前一个“值”之间发生变化时,该标志具有非空值(1)。


WITH cte AS (
SELECT *,
CASE WHEN NOT LAG(Value) OVER(ORDER BY Order) = Value THEN 1 END AS change_flag
FROM tab
)
SELECT order,
value,
COUNT(change_flag) OVER(ORDER BY order) + 1
FROM cte

更多回答

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