【发布时间】:2021-11-14 05:36:58
【问题描述】:
我有一张桌子statistic。该表具有step_index,例如1、2、3 和4。我需要让查询添加week2 和total_request2 其中星期是37 并加入具有相同step_index 的self 表。
例子:
step_index|type |year|week|total_request|
----------+--------+----+----+-------------+
1|Blasting|2021| 38| 1|
2|Blasting|2021| 38| 1|
3|Blasting|2021| 38| 1|
4|Blasting|2021| 38| 1|
1|Blasting|2021| 37| 6|
2|Blasting|2021| 37| 6|
3|Blasting|2021| 37| 6|
4|Blasting|2021| 37| 6|
结果应该是:
step_index|type |year|week|total_request|week2|total_request2|
----------+--------+----+----+-------------+-----+--------------+
1|Blasting|2021| 38| 1| 37| 6|
2|Blasting|2021| 38| 1| 37| 6|
3|Blasting|2021| 38| 1| 37| 6|
4|Blasting|2021| 38| 1| 37| 6|
我尝试了内部连接和 with 子句,但没有结果 week2 和 total_request2。
【问题讨论】:
-
如果有第三周,第 39 周,那么你期望 week3 和 total_request3 ?
-
不,我期待第 38 周和第 37 周
标签: sql postgresql join