Postgres 使用条件组子查询将行合并到一个数组中

问题描述 投票:0回答:0
SELECT json_object(array_agg(product_store), array_agg(json::text))as price_log FROM (
SELECT product_store, array_agg(array["Logs".product_price::text, "Logs".last_updated::text]) as json
                     FROM "Logs"
                     WHERE "Logs".product_id = 13
                     GROUP BY "Logs".product_store
) as price_log

输出

{
   "BestBuy.com":"{{24.99,\"2023-04-08 00:20:13.756603+00\"},{39.99,\"2023-04-14 22:57:56.003837+00\"},{39.99,\"2023-04-15 02:42:55.190658+00\"},{39.99,\"2023-04-15 16:57:55.045572+00\"},{39.99,\"2023-04-15 18:42:53.294265+00\"},{24.99,\"2023-04-08 02:57:51.220209+00\"},{24.99,\"2023-04-08 03:12:50.504241+00\"},{24.99,\"2023-04-08 05:57:50.356346+00\"},{24.99,\"2023-04-08 07:27:52.21909+00\"},{39.99,\"2023-04-16 00:42:52.578908+00\"},{39.99,\"2023-04-16 19:12:55.789055+00\"},{34.99,\"2023-04-25 00:57:55.631403+00\"},{24.99,\"2023-04-08 00:57:51.861773+00\"},{24.99,\"2023-04-08 02:27:54.083473+00\"},{24.99,\"2023-04-08 14:57:51.008387+00\"},{39.99,\"2023-04-18 11:57:54.997804+00\"},{39.99,\"2023-04-18 05:42:55.248361+00\"},{39.99,\"2023-04-18 12:12:54.926158+00\"},{34.99,\"2023-04-28 21:43:07.330042+00\"},{34.99,\"2023-04-25 23:12:58.103744+00\"},{34.99,\"2023-04-28 21:58:06.652304+00\"},{39.99,\"2023-04-20 06:42:55.641028+00\"},{34.99,\"2023-04-21 23:57:57.777371+00\"},{34.99,\"2023-04-20 15:12:56.185695+00\"},{34.99,\"2023-04-21 21:27:58.06414+00\"},{34.99,\"2023-04-22 04:27:58.140495+00\"},{34.99,\"2023-04-28 23:28:07.284809+00\"},{34.99,\"2023-04-29 00:58:06.053093+00\"},{24.99,\"2023-04-09 17:12:52.552401+00\"},{34.99,\"2023-04-22 01:27:58.035003+00\"},{34.99,\"2023-04-26 07:57:57.272001+00\"},{34.99,\"2023-04-22 00:27:56.772623+00\"},{34.99,\"2023-04-21 05:57:57.145709+00\"},{34.99,\"2023-04-29 03:28:06.034023+00\"},{39.99,\"2023-04-10 05:12:52.093764+00\"},{34.99,\"2023-04-21 21:57:59.336833+00\"},{34.99,\"2023-04-21 22:27:58.280576+00\"},{34.99,\"2023-04-28 21:28:05.418201+00\"},{39.99,\"2023-04-11 21:27:53.44626+00\"},{34.99,\"2023-04-22 15:27:56.237947+00\"},{34.99,\"2023-04-23 20:27:57.577541+00\"},{39.99,\"2023-04-13 13:57:52.456967+00\"},{39.99,\"2023-04-14 01:27:55.291552+00\"},{39.99,\"2023-04-14 21:27:58.342033+00\"},{39.99,\"2023-04-14 21:57:57.407943+00\"},{34.99,\"2023-04-29 06:43:06.139272+00\"},{34.99,\"2023-04-29 08:13:04.733555+00\"},{34.99,\"2023-04-29 13:13:06.21473+00\"},{34.99,\"2023-04-29 18:28:04.24191+00\"},{19.99,\"2023-04-30 05:43:02.277314+00\"},{19.99,\"2023-05-03 21:43:03.169792+00\"},{19.99,\"2023-05-08 20:13:07.710327+00\"},{19.99,\"2023-05-08 21:58:07.183089+00\"},{19.99,\"2023-05-10 07:13:07.101684+00\"}}",
   "Target.com":"{{39.99,\"2023-04-11 00:53:57.742973+00\"},{39.99,\"2023-04-14 03:15:01.237546+00\"},{39.99,\"2023-04-13 23:45:03.201827+00\"},{39.99,\"2023-04-14 00:00:01.635091+00\"},{33.99,\"2023-05-03 02:11:36.148637+00\"},{33.99,\"2023-05-03 02:17:06.085251+00\"}}"
}

也许我以错误的方式解决这个问题,我正在尝试组合与产品 ID 关联的“日志”行,因为这是在子查询中使用的,我需要将此查询放在一个数组中,因为有多次出现多个结果,如上。

我想要的结果是:

{
"BestBuy.com":{
 "Data:{
 "24.99,\"2023-04-08 00:20:13.756603+00\",
 "24.99,\"2023-04-08 00:20:13.756603+00\"
 }
},
"Target":{
  "Data": {
   "24.99,\"2023-04-08 00:20:13.756603+00\",
   "24.99,\"2023-04-08 00:20:13.756603+00\"
  }
}
}
arrays json postgresql multidimensional-array
© www.soinside.com 2019 - 2024. All rights reserved.