Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda