Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare and print data from two columns in sql

I have an employee table(emp). Sample data from emp table:

emp_name contact1 contact2
Harish  123      123
Manish  345      567
Ganesh  678      678

And the output I want is like below:

Harish  123
Ganesh  678
Manish  345
Manish  567

Explanation: It is like each employee has two contact number. If contact1 and contact2 are similar
just return the output in a row but if in case contact1 and contact2 are different then return name and contact in two different rows as shown for emp_name "Manish".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any suggestions on how this can be done ?

>Solution :

You can do a UNION over the selection of the two columns separately, as follows:

SELECT emp_name, contact1 AS contact
FROM emp
UNION
SELECT emp_name, contact2 AS contact
FROM emp

Check the demo here.

Note: if you want, you can add an ORDER BY clause at the end to sort your rows.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading