【发布时间】:2018-11-01 11:52:32
【问题描述】:
我有一个数据库视图。
此时它返回在id 列中可以有重复条目的行。
我想做以下事情:
SELECT DISTINCT ON (id) USING DESCRIBED BELOW RULE -- of course it is not valid sql
id
type
type 列可以有 3 个值:
xyz
如果存在id相同但type不同的行,DISTINCT的规则如下:
- 首先选择类型为
x的行 - 如果没有
x,则选择类型为y的行 - 如果没有
x且没有y,则选择类型为z的行
所以如果我有 3 行:
id type
1 'y'
1 'x'
2 'z'
预期的结果将是
id type
1 'x'
2 'z'
有没有办法做到这一点?
【问题讨论】:
标签: database postgresql distinct database-view