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?
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:That is, if
BUG_IDhas a greater value thanLINKED_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)), andDISTINCTmakes sure there’s none in the final result.