I am writing a code to look for duplicated Sub_brand_Descriptions in the same table having the same brand_code. I was able to produce the table below (which is what I need) however the first row and second row is the same because I am joining the table with itself. Is there a way to remove any of the duplicated row as distinct will not work (Different sub-brand id in the same column but because it appears in the row below in a different column, technically they are the same)?
select distinct brands.BRAND_ID as Brand_Code, sub.SUB_BRAND_ID as Sub_Brand_ID1, sub.SUB_BRAND as Sub_Brand_Descrption1, sub2.SUB_BRAND_ID as Sub_Brand_ID2, sub2.SUB_BRAND as Sub_Brand_Descrption2 from table1 as brands inner join table2 as sub on sub.BRAND_ID = brands.BRAND_ID and sub.LANGU = 'E' inner join table2 as sub2 on sub2.SUB_BRAND = sub.SUB_BRAND and sub2.LANGU = 'E' where sub.SUB_BRAND_ID != sub2.SUB_BRAND_ID and sub.BRAND_ID = sub2.BRAND_ID | Brand_Code | Sub_Brand_ID1 | Sub_Brand_Descrption1 | Sub_Brand_ID2 | Sub_Brand_Descrption2 |
|---|---|---|---|---|
| ABC | X123 | X123ABC | Y123 | X123ABC |
| ABC | Y123 | X123ABC | X123 | X123ABC |
Desired output:
| Brand_Code | Sub_Brand_ID1 | Sub_Brand_Descrption1 | Sub_Brand_ID2 | Sub_Brand_Descrption2 |
|---|---|---|---|---|
| ABC | X123 | X123ABC | Y123 | X123ABC |
Source data: Table 1:
| Brand_ID | label |
|---|---|
| ABC | 1 |
| CDE | 1 |
| EFG | 2 |
source Table 2:
| Brand_ID | Sub_Brand_ID | Sub_Brand | Language |
|---|---|---|---|
| ABC | X123 | X123ABC | E |
| ABC | Y123 | X123ABC | E |
| BBC | X223 | H23ABC | E |
| BBC | Y223 | H23ABC | E |

select distinctSub_Brand_IDvalues for oneSub_Brandvalue?