How to remove duplicated/corresponding values from table?

Slinky

I have a table structure similar to this:

send_id | rec_id

It is populated with the following sample data:

id1 | id2

id2 | id1

id3 | id1

id3 | id2

id4 | id1

id1 | id4

My attempt was using this query on id1:

SELECT * from inbox WHERE (send_id="id1" OR rec_id="id1") GROUP BY send_id,rec_id

but it doesn't work correctly, instead it shows:

id1 | id2

id2 | id1

id3 | id1

id1 | id3

id4 | id1

I would like the output to be something like this:

id1 | id2

id3 | id1

id4 | id1

I can see what's going wrong, but how do I fix it?

Gordon Linoff

This is a good place to use the least() and greatest() functions:

SELECT least(send_id, rec_id) as id1, greatest(send_id, rec_id) as id2
from inbox
WHERE send_id = 'id1' OR rec_id = 'id1'
GROUP BY least(send_id, rec_id), greatest(send_id, rec_id);

Or, if you prefer:

SELECT distinct least(send_id, rec_id) as id1, greatest(send_id, rec_id) as id2
from inbox
WHERE send_id = 'id1' OR rec_id = 'id1';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove accents from values in columns?

Remove values from a javascript table

How to remove duplicate values from an array in PHP

How to remove "Values" and "series" from highcharts?

How to remove duplicate values from multiple select?

How to remove values from a list of list in Python

How to properly remove NaN values from table

How to remove duplicate values from a multidimensional array?

How to remove the null values from the json input

R: How to remove values from a table that appear in another table?

How to Remove Row from Table

Remove correlation values from table if they are not significant (in another table)

How to remove count values "0" from result table?

How to remove duplicate values from a HashMap

How to remove values from dictionary that are not numbers in Python?

How to get values from table

How to remove values of a table? (MVN, Spring, Java)

How to remove " from the whole table?

How remove from group in table

How can I remove duplicate rows from a table but keeping the summation of values of a column

How to remove the index values from a json object

How to Remove Duplicates Values from table

Cannot remove duplicate values from a mysql table

How to remove duplicates from the dictionary if values repeats

How to remove values from Shared

How to remove NaN and Inf values from data.table where all columns are character types in R

How to remove duplicate values from the python dataframe?

Remove extra generated td values from the table

How to remove NaN values from pivot table only if each column has more than x NaN values?