Advertisement
gallopelado

lindo query trayendo json

Jun 4th, 2021
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT
  2.     DISTINCT(exg.nombre_grupo_examination)
  3.     , (SELECT
  4.             array_to_json(array_agg(row_to_json(d)))::jsonb
  5.         FROM (
  6.             SELECT
  7.                 ex.nombre_item_examination
  8.             FROM
  9.                 public.examination_item ex
  10.             WHERE ex.codigo_grupo_examination = exg.codigo_grupo_examination
  11.         ) d ) items
  12. FROM
  13.     public.examination_item exi
  14. LEFT JOIN public.examination_grupo exg ON exg.codigo_grupo_examination = exi.codigo_grupo_examination
  15. --******---
  16. SELECT
  17.     array_to_json(array_agg(row_to_json(subq)))::text json_data
  18. FROM (
  19.     SELECT
  20.         DISTINCT(exg.nombre_grupo_examination)
  21.         , (SELECT
  22.                 array_to_json(array_agg(row_to_json(d)))::jsonb
  23.             FROM (
  24.                 SELECT
  25.                     ex.codigo_grupo_examination
  26.                     , ex.codigo_item_examination
  27.                     , ex.nombre_item_examination
  28.                     , CASE WHEN (TRIM(ex.unit_examination) = '') THEN null
  29.                     WHEN (TRIM(ex.unit_examination) IS NULL) THEN null
  30.                     ELSE CONCAT('[', ex.unit_examination, ']') END unit_examination
  31.                 FROM
  32.                     public.examination_item ex
  33.                 WHERE ex.codigo_grupo_examination = exg.codigo_grupo_examination
  34.             ) d ) items
  35.     FROM
  36.         public.examination_item exi
  37.     LEFT JOIN public.examination_grupo exg ON exg.codigo_grupo_examination = exi.codigo_grupo_examination
  38. ) subq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement