我的表格中的行按排名排序。所有 rank 值均不同。另外,我还有颜色列,其中包含可能的值(红色,蓝色,绿色)。我需要遍历earch行(例如,将其命名为 X )并检查下一个 n 行中是否存在行(例如,将其命名为 Y )其中 X.color == Y.color 然后需要移动 Y 行并将其插入 X 行之后。 例如 n = 5 和起始表:
id rank color
1 0.8 red
3 0.76 blue
4 0.75 blue
7 0.743 green
5 0.742 red
6 0.741 green
8 0.6 blue
9 0.5 blue
11 0.47 green
12 0.45 blue
10 0.4 red
1进行迭代,在接下来的 n 行中,我们有颜色为红色的行,然后:
id rank color
1 0.8 red
5 0.742 red
3 0.76 blue
4 0.75 blue
7 0.743 green
6 0.741 green
8 0.6 blue
9 0.5 blue
11 0.47 green
12 0.45 blue
10 0.4 red
2进行迭代,接下来的 n 行中没有红色,因此没有更改
3进行迭代,在接下来的 n 行中有 blue ,但是在当前行之后已经有了下一个蓝色行。
4迭代,接下来的 n 行中,我们有蓝色,然后:
id rank color
1 0.8 red
5 0.742 red
3 0.76 blue
4 0.75 blue
8 0.6 blue
7 0.743 green
6 0.741 green
9 0.5 blue
11 0.47 green
12 0.45 blue
10 0.4 red
5进行迭代,我们在接下来的 n 行中添加蓝色,然后:
id rank color
1 0.8 red
5 0.742 red
3 0.76 blue
4 0.75 blue
8 0.6 blue
9 0.5 blue
7 0.743 green
6 0.741 green
11 0.47 green
12 0.45 blue
10 0.4 red
6迭代,接下来的 n 行中,我们有蓝色,然后:
id rank color
1 0.8 red
5 0.742 red
3 0.76 blue
4 0.75 blue
8 0.6 blue
9 0.5 blue
12 0.45 blue
7 0.743 green
6 0.741 green
11 0.47 green
10 0.4 red
7进行迭代,在接下来的 n 行中有 green ,但是在当前行之后已经有下一个绿色行。
8进行迭代,在接下来的 n 行中有 green ,但是在当前行之后已经有下一个绿色行。
9进行迭代,在接下来的 n 行中有 green ,但是在当前行之后已经有下一个绿色行。
10不进行任何更改。
有人对如何解决这个问题有一个想法,我试图固定窗口功能,但尚不清楚是否可以借助它们来完成,也许还需要其他机制吗?
答案 0 :(得分:1)
对于刚接触SQL的人们来说,遇到诸如此类的问题时考虑迭代确实是很常见的做法,但是无需任何迭代就可以做到这一点。让我们分解一下。首先,让我们根据排名列找到所有数据的row_id:
with data as (select * from (values
(1,0.8 ,'red'),
(3,0.76 ,'blue'),
(4,0.75 ,'blue'),
(7,0.743 ,'green'),
(5,0.742 ,'red'),
(6,0.741 ,'green'),
(8,0.6 ,'blue'),
(9,0.5 ,'blue'),
(11,0.47 ,'green'),
(12,0.45 ,'blue'),
(10,0.4 ,'red')
) v(id, rank, color)
)
select id, rank, color,
row_number() over (order by rank desc) as row_id
FROM data
ORDER BY rank desc;
id | rank | color | row_id
----+-------+-------+--------
1 | 0.8 | red | 1
3 | 0.76 | blue | 2
4 | 0.75 | blue | 3
7 | 0.743 | green | 4
5 | 0.742 | red | 5
6 | 0.741 | green | 6
8 | 0.6 | blue | 7
9 | 0.5 | blue | 8
11 | 0.47 | green | 9
12 | 0.45 | blue | 10
10 | 0.4 | red | 11
(11 rows)
从那里,我们可以找出前五行中具有相同颜色的最小行:
with data as (select * from (values
(1,0.8 ,'red'),
(3,0.76 ,'blue'),
(4,0.75 ,'blue'),
(7,0.743 ,'green'),
(5,0.742 ,'red'),
(6,0.741 ,'green'),
(8,0.6 ,'blue'),
(9,0.5 ,'blue'),
(11,0.47 ,'green'),
(12,0.45 ,'blue'),
(10,0.4 ,'red')
) v(id, rank, color)
), all_rows as (
select id, rank, color,
row_number() over (order by rank desc) as row_id
FROM data
ORDER BY rank desc
)
select id, rank, color, row_id,
first_value(row_id) over (partition by color order by row_id range between 5
preceding and current row)
from all_rows
order by 5, rank desc;
id | rank | color | row_id | first_value
----+-------+-------+--------+-------------
1 | 0.8 | red | 1 | 1
5 | 0.742 | red | 5 | 1
3 | 0.76 | blue | 2 | 2
4 | 0.75 | blue | 3 | 2
8 | 0.6 | blue | 7 | 2
9 | 0.5 | blue | 8 | 3
7 | 0.743 | green | 4 | 4
6 | 0.741 | green | 6 | 4
11 | 0.47 | green | 9 | 4
12 | 0.45 | blue | 10 | 7
10 | 0.4 | red | 11 | 11
(11 rows)
这很接近,但是不太正确。我们需要从每种颜色的最后5行中获取最小值。我们还需要一步:
with data as (select * from (values
(1,0.8 ,'red'),
(3,0.76 ,'blue'),
(4,0.75 ,'blue'),
(7,0.743 ,'green'),
(5,0.742 ,'red'),
(6,0.741 ,'green'),
(8,0.6 ,'blue'),
(9,0.5 ,'blue'),
(11,0.47 ,'green'),
(12,0.45 ,'blue'),
(10,0.4 ,'red')
) v(id, rank, color)
), all_rows as (
select id, rank, color,
row_number() over (order by rank desc) as row_id
FROM data
ORDER BY rank desc
), first_values as (
select id, rank, color, row_id,
first_value(row_id) over (partition by color order by row_id range between 5
preceding and current row)
from all_rows
order by 5, rank desc
)
select id, rank, color, row_id,
first_value(first_value) over (partition by color order by row_id range between 5 preceding and current row) as overall_rank
FROM first_values
ORDER BY overall_rank, rank desc
;
id | rank | color | row_id | overall_rank
----+-------+-------+--------+--------------
1 | 0.8 | red | 1 | 1
5 | 0.742 | red | 5 | 1
3 | 0.76 | blue | 2 | 2
4 | 0.75 | blue | 3 | 2
8 | 0.6 | blue | 7 | 2
9 | 0.5 | blue | 8 | 2
12 | 0.45 | blue | 10 | 2
7 | 0.743 | green | 4 | 4
6 | 0.741 | green | 6 | 4
11 | 0.47 | green | 9 | 4
10 | 0.4 | red | 11 | 11
(11 rows)