如何消除 Oracle-SQL-Table 中含义相同的值?
我有一个这样的表:
ID | Val1 | Val2
---------------------
1 | 1 | 2
2 | 1 | 3
3 | 2 | 1
4 | 2 | 3
5 | 3 | 1
6 | 3 | 2
现在我的问题是,1 - 2 的含义与 2 - 1 相同(例如,查看@ ID 1 和 ID 3),我想消除值 1 - 值 2 含义的所有条目与 value 2 - value1 相同(希望你能遵循我的逻辑)。
I've got a table like this:
ID | Val1 | Val2
---------------------
1 | 1 | 2
2 | 1 | 3
3 | 2 | 1
4 | 2 | 3
5 | 3 | 1
6 | 3 | 2
now my problem is, that 1 - 2 means the same like 2 - 1 (look @ ID 1 and ID 3 for example) and I want to eliminate all entries where value 1 - value 2 means the same like value 2 - value1 (hope you could follow my logic in here).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么样:
我任意保留ID值最大的行。
示例:
轻松适应保留不同的行,例如
UPDATE:
想不出一个真正聪明的方法,所以这是回答评论中问题的蛮力方法:
How about this:
I arbitrarily kept the row with the greatest ID value.
Example:
Easily adaptable to keep different rows, e.g.,
UPDATE:
Couldn't think of a really clever way, so here's the brute force method to answer the question in your comment:
我不记得这个语法在 Oracle 中是否有效(主要是使用 DELETE 主题的别名),但你可以尝试这个:
由于列的顺序似乎并不重要,我会做一个任意决定,我会向表添加一个约束来检查 val1 <值2。然后,您可以对两列的组合设置唯一约束(如果您还没有),并确保不会再次遇到此问题。
当然,您还需要确保向表中插入行的任何应用程序或代码都知道该约定(val1 应始终是两个值中最小的一个)并遵循该约定。
I don't recall if this syntax is valid in Oracle or not (mostly the use of an alias for the DELETE subject), but you can try this:
Since the ordering of the columns doesn't seem to matter, I would make an arbitrary decision and I would add a constraint to the table to check that val1 < val2. You can then put a unique constraint on the combination of the two columns (if you don't already have one) and be sure that you won't have this problem again.
Of course, you would also need to make sure that any application or code that inserts rows into the table knows that convention (val1 should always be the smallest of the two values) and follows it.
如果消除意味着不显示,请尝试此操作,这不会返回这些条件的任何行
If eliminate means not show, try this, this won't return any rows for those conditions
这里有一个没有处理好的极端情况。即 Val1 和 Val2 相等的情况。删除除出现一次之外的所有此类行有点棘手。有人有什么想法吗?
There is a corner case that isn't handled well here. That is the case where Val1 and Val2 are equal. Deleting all but one occurrence of such rows is a bit trickier. Anyone have any ideas?