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 create 2 columns using data from 1 column and merging them

I’m facing the some problems in big query, the single column could not separate into 2 columns. I want the column index with 8 and 10 to be new columns called universal_id and project_id using the value in the column "value".

My current table is:

user_id | index | value
a.      | 1.    | 123
b.      | 8.    | 456
c.      | 10.   | 12.60
b.      | 10.   | 789

I want the result to be this:

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

user_id | project_id | universal_id | 
a       | NA         | NA
b.      | 789        | 456
c.      | 12.60      | NA

I have tried this, but it does not work. I searched a lot of places, and could find the answer I am looking for. Any help would be greatly appreciated. Thank you in advance!!!

select user_id,
case when index = 8 then value else null end as universal_id,
case when index = 10 then value else null end as ps_project_id

from test_1

>Solution :

You may use conditional aggregation here:

SELECT
    user_id,
    MAX(CASE WHEN index = 10 THEN value END) AS project_id,
    MAX(CASE WHEN index = 8  THEN value END) AS universal_id
FROM test_1
GROUP BY user_id;
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