PostgreSQL 复制具有两列重复的行
PostgreSQL duplicating rows with two column duplicates
在 PostgreSQL 中,我正在寻找以下问题的答案。
有两列提供有关 'start' 和 'end' 的数据,还有一个 'date' 列。目前日期列只存在一次,'start' 和 'end' 充满了可能性。
我正在寻找创建具有唯一值但具有重复日期的 'start' 和 'end' 列的可能性。
当前:
id date start end
1 2017-03-13 a [null]
2 2017-03-14 [null] a
3 2017-03-14 b [null]
4 2017-03-16 [null] b
5 2017-03-16 c c
希望:
id date start end
1 2017-03-13 a [null]
2 2017-03-14 [null] a
3 2017-03-14 b [null]
4 2017-03-16 [null] b
5 2017-03-16 c [null]
6 2017-03-16 [null] c
有人有想法吗?
如果我对你的问题理解正确,并且你想要设置 start
和 "end"
之一,并且与 date
的组合是唯一的,你可以这样做:
ALTER TABLE tab
ADD CHECK(start IS NULL AND "end" IS NOT NULL
OR start IS NOT NULL AND "end" IS NULL);
CREATE UNIQUE INDEX ON tab (date, COALESCE(start, "end"));
在 PostgreSQL 中,我正在寻找以下问题的答案。 有两列提供有关 'start' 和 'end' 的数据,还有一个 'date' 列。目前日期列只存在一次,'start' 和 'end' 充满了可能性。
我正在寻找创建具有唯一值但具有重复日期的 'start' 和 'end' 列的可能性。
当前:
id date start end
1 2017-03-13 a [null]
2 2017-03-14 [null] a
3 2017-03-14 b [null]
4 2017-03-16 [null] b
5 2017-03-16 c c
希望:
id date start end
1 2017-03-13 a [null]
2 2017-03-14 [null] a
3 2017-03-14 b [null]
4 2017-03-16 [null] b
5 2017-03-16 c [null]
6 2017-03-16 [null] c
有人有想法吗?
如果我对你的问题理解正确,并且你想要设置 start
和 "end"
之一,并且与 date
的组合是唯一的,你可以这样做:
ALTER TABLE tab
ADD CHECK(start IS NULL AND "end" IS NOT NULL
OR start IS NOT NULL AND "end" IS NULL);
CREATE UNIQUE INDEX ON tab (date, COALESCE(start, "end"));