SQL Duplicate Removal Request

On my desk is called distance. It has 4 columns. id, start_from, end_to and distance .

I have duplicate entries. Duplicate records in the sense

start_from   |   end_to    | distance
Chennai        Bangalore     350
Bangalore      Chennai       350
Chennai        Hyderabad     500
Hyderabad      Chennai       510

In the table above, chennai to bangalore and bangalore to chennai both have the same distance . Therefore, I need a request to delete this entry on select .

I want to be posted as

start_from   |   end_to    | distance
Chennai        Bangalore     350
Chennai        Hyderabad     500
Hyderabad      Chennai       510
+4
source share
4 answers

If there are no differences between Chennai to Bangaloreor Bangalore to Chennainot, you can try the following:

select
    max(`start_from`) as `start_from`,
    min(`end_to`) as `end_to`,
    `distance`
from yourtable
group by
    case when `start_from` > `end_to` then `end_to` else `start_from` end,
    case when `start_from` > `end_to` then `start_from` else `end_to` end,
    `distance`

demo .

Chennai to Hyderabad 350, demo.

, Bangalore to Chennai , max min:

select
    min(`start_from`) as `start_from`,
    max(`end_to`) as `end_to`,
    `distance`
from yourtable
group by
    case when `start_from` > `end_to` then `end_to` else `start_from` end,
    case when `start_from` > `end_to` then `start_from` else `end_to` end,
    `distance`

demo.

case when .

+2

:

SELECT LEAST(start_from, end_to) AS start_from, 
       GREATEST(start_from, end_to) AS end_to, 
       distance
FROM mytable 
GROUP BY LEAST(start_from, end_to), GREATEST(start_from, end_to), distance
HAVING COUNT(*) > 1

:

start_from,   end_to,  distance
--------------------------------
Bangalore,    Chennai, 350

:

SELECT t1.*
FROM mytable AS t1
LEFT JOIN (
    SELECT LEAST(start_from, end_to) AS start_from, 
           GREATEST(start_from, end_to) AS end_to, 
           distance
    FROM mytable 
    GROUP BY LEAST(start_from, end_to), GREATEST(start_from, end_to), distance
    HAVING COUNT(*) > 1
) AS t2 ON t1.start_from = t2.start_from AND 
           t1.end_to = t2.end_to AND 
           t1.distance = t2.distance    
WHERE t2.start_from IS NULL

WHERE, t2.start_from IS NULL, .

:

start_from  end_to     distance
--------------------------------
Chennai     Bangalore  350
Chennai     Hyderabad  500
Hyderabad   Chennai    510
+2

( ) :

select distinct
    case when start_from  > end_to then end_to     else  start_from end as _start,
    case when start_from  > end_to then start_from else  end_to     end as _end,
    distance
from distance;

:

+-----------+-----------+----------+
| _start    | _end      | distance |
+-----------+-----------+----------+
| Bangalore | Chennai   |      350 |
| Chennai   | Hyderabad |      500 |
| Chennai   | Hyderabad |      510 |
+-----------+-----------+----------+
0

,

id  start_from              end_to                  distance
0   Chennai                 Bangalore               350
1   Bangalore               Chennai                 350
2   Chennai                 Hyderabad               500
3   Hyderabad               Chennai                 510

id.

Select 
    O.start_from,
    O.end_to,
    O.distance 
From 
    distance O
Left Join
    distance P
On 
    1 = 1
    and O.start_from = P.end_to 
    and O.end_to = P.start_from
Where 
    1 = 1
    and O.distance <> P.distance 
    or(O.distance = P.distance and O.id < P.id)
0

Source: https://habr.com/ru/post/1675773/


All Articles