Eliminate duplicate results in a select query that contains CLOB column

徘徊边缘 提交于 2019-12-12 02:13:12

问题


This is the select query:

select orderid,ordernum,orderdate,orderxml from orders

The query returns multiple rows with same ordernum. I tried to use DISTINCT and Group BY but orderxml is a clob column. How can I eliminate duplicate ordernums in my query?


回答1:


You could use an analytic function to identify a single orderid for each ordernum - probably either min or max, but other functions are available, it depends on what you need - in a subquery, and then filter to only get the rows with the identified IDs:

select orderid, ordernum, orderdate, orderxml
from (
  select orderid, ordernum, orderdate, orderxml,
    max(orderid) over (partition by ordernum) as maxorderid
  from orders
)
where orderid = maxorderid;

The inline view gets all the columns and rows from your table, but adds an additional column to its result that has the maximum ID across all rows with the same order number. (You can add any other filters you want in there, or course).

The outer filter then only matches the ID for each order number that matches that maximum value.

This assumes that orderid is unique - at least for an ordernum but presumably globally. You said the orderdate is always the same for the ordernum but you could include that in the partition by if not, possibly modifying the analytic function used.

(And then investigate how and why you are getting duplicates, and try to stop them; then remove the duplicates from your table - carefully...)



来源:https://stackoverflow.com/questions/42604897/eliminate-duplicate-results-in-a-select-query-that-contains-clob-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!