我有一个查询结果在 table1
-------------------------
|column1|column2|column3|
-------------------------
| v1 | 30 | 40 |
| v1 | 34 | 41 |
| v1 | 35 | 42 |
| v2 | 30 | 40 |
| v2 | 34 | 41 |
| v2 | 35 | 42 |
-------------------------
我想将第一列中的重复值更改为 NULL
,(即)。
-------------------------
|column1|column2|column3|
-------------------------
| v1 | 30 | 40 |
| null | 34 | 41 |
| null | 35 | 42 |
| v2 | 30 | 40 |
| null | 34 | 41 |
| null | 35 | 42 |
-------------------------
我应该如何处理 table1?
最佳答案
您可以使用row_number()
函数:
select (case when row_number() over (partition by col1 order by col1) > 1
then null else col1 end) col1,
col2, col3
from table t;
不过,你也可以使用lag()
函数
select (case when lag(col1) over (partition by col1 order by col1) = col1
then null else col1 end) col1,
col2, col3
from table t;
关于sql - 如何重写查询中的某些值? - PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50055703/