Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
How to extract different json elements from the same table in SQL query?

I am querying from a table with the following format:

id|provider|score
--------------------------------
1 |  att     | '{"attscore":300}'
1 |  verizon | '{"verizonscore":299}'
2 |   att    | '{"attscore":200}'
3 |  verizon | '{"verizonscore":155}'

I am trying to get a table that looks like the following:

id|attscore|verizonscore
-------------------------
1  |  300   |    299
2  |  200   |    null
3  |  null  |    155

Note that used to json in sql

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

CREATE TABLE table1 (
  `id` INTEGER,
  `provider` VARCHAR(7),
  `score` VARCHAR(22)
);

INSERT INTO table1
  (`id`, `provider`, `score`)
VALUES
  ('1', 'att', '{"attscore":300}'),
  ('1', 'verizon', '{"verizonscore":299}'),
  ('2', 'att', '{"attscore":200}'),
  ('3', 'verizon', '{"verizonscore":155}');
SELECT 
id,
GROUP_CONCAT(CASE WHEN provider = 'att' THEN `score`->"$.attscore" ELSe NULL END) attscore
,GROUP_CONCAT(CASE WHEN provider = 'verizon' THEN `score`->"$.verizonscore" ELSe NULL END) verizonscore
FROM table1
GROUP BY id
id | attscore | verizonscore
-: | :------- | :-----------
 1 | 300      | 299         
 2 | 200      | null        
 3 | null     | 155         

db<>fiddle here

This works with a fixed number of column quite well, if you have much more of these you need to do something like this

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!