说我的PostgreSQL数据库中有下表:
if target.column=11 then 'k
if target.value=1 then
cells(target.row,"C") '...whatever
cells(target.row,"F") '...whatever
cells(target.row,"G") '...whatever
end if
end if
我现在将这个表与其自身一起列在document_id列上,如下所示:
id|user_id|document_id|
--|-------|-----------|
1|10 | 100|
2|20 | 100|
3|10 | 200|
4|20 | 200|
5|10 | 300|
6|20 | 300|
7|10 | 400|
8|20 | 400|
结果:
select t1.document_id,t1.user_id as user_id1,t2.user_id as user_id2
from test_table t1 left join test_table t2 on (t1.document_id = t2.document_id and t1.user_id <> t2.user_id);
在这里,我想删除类似的行,例如下面的行,因为两条记录都表示同一件事:
document_id|user_id1|user_id2|
-----------|--------|--------|
100|10 |20 |
100|20 |10 |
200|10 |20 |
200|20 |10 |
300|10 |20 |
300|20 |10 |
400|10 |20 |
400|20 |10 |
因此,预期结果应类似于:
document_id|user_id1|user_id2|
-----------|--------|--------|
100|10 |20 |
100|20 |10 |
所以我基本上要求document_id出现一次而不是两次。有什么办法吗?
编辑:
我尝试了@jarlh建议的以下查询:
document_id|user_id1|user_id2|
-----------|--------|--------|
100|10 |20 |
200|10 |20 |
300|10 |20 |
400|10 |20 |
但是结果是当user_id1大于两者时,user_id2为null:
select t1.document_id,t2.user_id as user_id2
from test_table t1 left join test_table t2 on (t1.document_id = t2.document_id and t1.user_id < t2.user_id);