PostgreSQL выводит несколько запросов в массив - PullRequest
0 голосов
/ 25 мая 2018

Работа в postgreSQL 9.6:

У меня есть несколько операторов выбора, каждый из которых выводит одну строку

например, выберите 8 как "ColA", выберите 20 как "ColB", выберите13 как "ColC"

И мне нужно получить их в массив, такой как:

["ColA",8],["ColB",20],["ColC",13]

Я пробовал много разновидностей row_to_json, array_to_json и т. Д., И без радости, кто-нибудь может помочьпожалуйста?


Фактический код, который я пробовал, немного сложнее, чем описано выше, но структура данных не такая ... вот мой существующий код (просто чтобы показать, что у меня есть

SELECT "JobTitle" "name",
    (

    select array_to_json(array_agg(row_to_json(d2))) from

    (SELECT 

    (select row_to_json(d2_1) from (select SUM(CASE WHEN "StartDateTime"<(_Date_For + '1 hour'::interval) AND "EndDateTime">(_Date_For) THEN "Effect" ELSE 0 END) "00:00" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_1) 

UNION ALL

(select row_to_json(d2_10) from (select SUM(CASE WHEN "StartDateTime"<(_Date_For + '11 hour'::interval) AND "EndDateTime">(_Date_For + '10 hour'::interval) THEN "Effect" ELSE 0 END) "10:00" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_10) 

UNION ALL

     (select row_to_json(d2_11) from (select SUM(CASE WHEN "StartDateTime"<(_Date_For + '12 hour'::interval) AND "EndDateTime">(_Date_For + '11 hour'::interval) THEN "Effect" ELSE 0 END) "11:00" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_11)

    ) d2

    ) as "data" 

    FROM "tmpDashboardData" x GROUP BY "JobTitle"

1 Ответ

0 голосов
/ 29 мая 2018

Спасибо, Фдавидов, я в конце концов решил это ... вот мой код для информации:

SELECT "JobTitle" "name",
    (
    select array_to_json(array_agg(row_to_json(d2))) from
    (SELECT  
     (select row_to_json(d2_1) from (select '00:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '1 hour'::interval) AND "EndDateTime">(_Date_For) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_1) UNION ALL
     (select row_to_json(d2_2) from (select '01:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '2 hour'::interval) AND "EndDateTime">(_Date_For + '1 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_2) UNION ALL
     (select row_to_json(d2_3) from (select '02:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '3 hour'::interval) AND "EndDateTime">(_Date_For + '2 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_3) UNION ALL
     (select row_to_json(d2_4) from (select '03:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '4 hour'::interval) AND "EndDateTime">(_Date_For + '3 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_4) UNION ALL
     (select row_to_json(d2_5) from (select '04:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '5 hour'::interval) AND "EndDateTime">(_Date_For + '4 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_5) UNION ALL
     (select row_to_json(d2_6) from (select '05:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '6 hour'::interval) AND "EndDateTime">(_Date_For + '5 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_6) UNION ALL
     (select row_to_json(d2_7) from (select '06:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '7 hour'::interval) AND "EndDateTime">(_Date_For + '6 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_7) UNION ALL
     (select row_to_json(d2_8) from (select '07:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '8 hour'::interval) AND "EndDateTime">(_Date_For + '7 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_8) UNION ALL
     (select row_to_json(d2_9) from (select '08:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '9 hour'::interval) AND "EndDateTime">(_Date_For + '8 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_9) UNION ALL
     (select row_to_json(d2_10) from (select '09:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '10 hour'::interval) AND "EndDateTime">(_Date_For + '9 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_10) UNION ALL
     (select row_to_json(d2_11) from (select '10:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '11 hour'::interval) AND "EndDateTime">(_Date_For + '10 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_11) UNION ALL
     (select row_to_json(d2_12) from (select '11:00' "0", SUM(CASE WHEN "StartDateTime"<(_Date_For + '12 hour'::interval) AND "EndDateTime">(_Date_For + '11 hour'::interval) THEN "Effect" ELSE 0 END) "1" from "tmpDashboardData" where "JobTitle"=x."JobTitle") d2_12)
    ) d2
    ) as "data" 
    FROM "tmpDashboardData" x GROUP BY "JobTitle"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...