Home > Blockchain >  remove repeated rows while self join in sql
remove repeated rows while self join in sql

Time:09-09

I am trying to remove repeated values from rows based on joins on same table but can't remove one value in one column in table1 mapped to same column in table2.

Table: Note: not restricted to only one date in table, there can be many with multiple sid

date    | sid | comp | disc
-----------------------
23 june | 1  | az  | 20
23 june | 1  | ph  | 22
23 june | 1  | mg  | 10
23 june | 2  | mg  | 8
23 june | 3  | ph  | 15
23 june | 3  | az  | 11
------------------------

ON SELF JOINING

select t1.*, t2.comp as comp1, t2.disc as disc1
from table as t1
left join table as t2 on t1.date = t2.date and t1.sid = t2.sid and t1.comp <> t2.comp
Output from above query:

date    | sid | comp | disc | comp1 | disc1
-------------------------------------------
23 june | 1  | az  | 20     | ph    | 22
23 june | 1  | az  | 20     | mg    | 10
23 june | 1  | ph  | 22     | az    | 20
23 june | 1  | ph  | 20     | mg    | 10
23 june | 1  | mg  | 10     | mg    | 10
23 june | 2  | mg  | 10     | null  | null
23 june | 3  | ph  | 10     | az    | 11
23 june | 3  | az  | 11     | ph    | 10

Expected result: (Here I am trying to fetch combination of comp & comp1 where for each sid & date, distinct comp values mapped with mg only & if sid has only mg or non-mg values then null in comp1) Note: No repeated rows when comp mapped to comp1

date    | sid | comp | disc | comp1 | disc1
-------------------------------------------
23 june | 1  | az  | 20     | mg    | 10
23 june | 1  | ph  | 20     | mg    | 10
23 june | 2  | mg  | 10     | null  | null
23 june | 3  | ph  | 10     | null  | null
23 june | 3  | az  | 11     | null  | null

CodePudding user response:

See db<>fiddle

WITH cte AS (
SELECT t1.*, t2.comp as comp1, t2.disc as disc1, SUM(1) OVER(PARTITION BY date,sid,comp) AS cnt
FROM `table` t1
LEFT JOIN `table` t2 ON t1.date = t2.date AND t1.sid = t2.sid AND t1.comp <> t2.comp 
)

SELECT 
  date, sid, comp, disc, 
  CASE WHEN comp1 <> 'mg' THEN NULL ELSE comp1 END AS comp1, 
  CASE WHEN comp1 <> 'mg' THEN NULL ELSE disc1 END AS disc1
FROM cte
WHERE
 (CASE WHEN comp <> 'mg' OR comp1 IS NULL THEN cnt END) = 1
 OR 
 (CASE WHEN comp <> 'mg' AND comp1 = 'mg' THEN cnt END) >= 2
  • Related