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

How to eliminate NULL values from the query result?

Currently I’m trying to write a single SQL query to produce some intended result, I managed to do it but there are some null values in my results and I’m just wondering how can I get rid of them.

Here is the database structure:
enter image description here

Here is my query:

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

SELECT ut.username,
       (case when FieldID = 1 then Data end) as phone,
       (case when FieldID = 2 then Data end) as email
FROM usertable ut
JOIN userdata ud
ON ut.ID = ud.userID;

Here is my result:

enter image description here

Here is the intended result:

enter image description here

Any suggestions are welcomed!

>Solution :

Use a pivot to get just one row per user.

SELECT ut.username,
       MAX(case when FieldID = 1 then Data end) as phone,
       MAX(case when FieldID = 2 then Data end) as email
FROM usertable ut
JOIN userdata ud
ON ut.ID = ud.userID
GROUP BY ut.username;
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