Viji
@Viji
DevOps Engineer

Можно ли упростить выражение sql, чтобы избежать такой вложенности?

WITH table_columns AS (
  SELECT table_name, column_name, data_type
  FROM information_schema.columns
  WHERE table_schema = 'public'
)
SELECT table_name, json_object_agg(element_number, temp_columns) as columns FROM
(SELECT
   table_name, element_number, json_object_agg(column_name, data_type) as temp_columns
FROM (SELECT
    table_name, ROW_NUMBER() OVER (PARTITION BY table_name ORDER BY column_name) AS element_number, column_name, data_type
    FROM table_columns) AS new_table
    GROUP BY table_name, element_number ORDER BY element_number) AS final_table
GROUP BY table_name;
  • Вопрос задан
  • 93 просмотра
Пригласить эксперта
Ответы на вопрос 1
iMedved2009
@iMedved2009
Не люблю людей
with t as(SELECT table_name, column_name, data_type, ROW_NUMBER() OVER (PARTITION BY table_name ORDER BY column_name) as num
  FROM information_schema.columns
  WHERE table_schema = 'public')
select json_agg(json_build_object(num, json_build_object(column_name, data_type))) from t group by table_name;
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы