我有一个简单的查询,它从具有几个条件的多个表中提取数据
select x, step, code, ....,
from
table 1
inner join table 2
inner join table 3
where
'filters'
order by x, code
这给了我一个表格,其中两列带有步骤和代码:
x | 步骤 | 代码 |
---|---|---|
1 | 0 | 0001 |
1 | 10 | 0000 |
1 | 20 | 0000 |
1 | 30 | 0000 |
1 | 40 | 0000 |
1 | 1000 | 0001 |
2 | 0 | 0000 |
2 | 10 | 0000 |
2 | 20 | 0000 |
如上,0步骤表示流程是否失败,子步骤具体表示是哪一部分。然后当 step 回到 0 时,它现在是一个新进程。
有没有办法提取代码的第 0 步值(因此在本例中为“0001”),然后将该值用作代码,直到到达下一步 0 并更新该值?
所以桌子现在看起来像这样:
x | 步骤 | 代码 |
---|---|---|
1 | 0 | 0001 |
1 | 10 | 0001 |
1 | 20 | 0001 |
1 | 30 | 0001 |
1 | 40 | 0001 |
1 | 1000 | 0001 |
2 | 0 | 0000 |
2 | 10 | 0000 |
2 | 20 | 0000 |
可以是一个新的专栏,为了节省时间,我只是标明了相同的。
感谢任何帮助,谢谢
您忘记了
ORDER BY
子句中的步骤,以便获得确定性结果,其中步骤 0 始终是 x 显示的第一步。
使用
FIRST_VALUE
获取每个x的步骤0的代码。
select x, step, code, first_value(code) over (partition by x order by step) as xcode
inner join table 2
inner join table 3
where <filters>
order by x, step, xcode;