Ignore redundant values fetched from database

孤街醉人 提交于 2019-12-11 07:29:37

问题


Following is the sample o/p of the SQL query -

  BUG_ID   |  LINKED_BUG_ID
-----------|-----------------
3726       |  45236
45236      |  3726
3726       |  45254
45254      |  3726
3726       |  45402
45402      |  3726
3726       |  1182
1182       |  55745

In my SQL o/p, there are two rows out of which one row is redundant. e.g Bug Id 3726 and Linked Bug Id 45326 and Bug Id 45326 and Linked Bug Id 3726 are present twice in the o/p, out of which we need only one row and ignore such kind of duplicate rows (having either of a value repeated in Bug Id column or Linked Bug Id column), without affecting the o/p containing the distinct value.

Currently I can identify such duplicate rows using following query, but I need only one single row out of such duplicate rows.

SELECT 
  BUG_ID, 
  LINKED_BUG_ID, 
  CASE 
    WHEN BUG_ID IN (select LINKED_BUG_ID FROM MY_BUG_LINKS) AND 
      LINKED_ISSUE_ID IN (SELECT BUG_ID FROM MY_BUG_LINKS) 
    THEN 'true' ELSE 'false'  
  END AS EQUAL 
FROM MY_BUG_LINKS;

Following is the SQL query that I use in my code for fetching all the rows (which includes even duplicate rows)

SELECT BUG_ID, LINKED_BUG_ID FROM MY_BUG_LINKS;

How can I avoid fetching redundant duplicate rows at the database level itself or in my java code?


回答1:


If this is merely about treating (B, A) as a duplicate of (A, B) and you do not particularly care whether the row returned will be (A, B) or (B, A), you could do something like this:

SELECT DISTINCT
  CASE WHEN BUG_ID > LINKED_BUG_ID THEN LINKED_BUG_ID ELSE BUG_ID AS BUG_ID,
  CASE WHEN BUG_ID > LINKED_BUG_ID THEN BUG_ID ELSE LINKED_BUG_ID AS LINKED_BUG_ID
FROM MY_BUG_LINKS;

That is, if BUG_ID has a greater value than LINKED_BIG_ID, the query swaps the two IDs, otherwise the values are returned unchanged. Therefore, (A, B) and (B, A) always produce duplicate rows (both would be either (A, B) or (B, A)), and DISTINCT makes sure there's none in the final result.




回答2:


you may try something similar to this:

select 
distinct bug_id from
(
    select bug_id as bug_id from TABLE
 union
    select linked_bug_id as bug_id from TABLE
)


来源:https://stackoverflow.com/questions/10073054/ignore-redundant-values-fetched-from-database

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