英文:
Postgres Combine Rows in into one array with Conditional Group Subquery
问题
Here is the translated content you requested:
{
"BestBuy.com": {
"data": [
[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": {
"data": [
[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
<details>
<summary>英文:</summary>
CREATE TABLE "Logs"(
id bigint NOT NULL,
product_id bigint,
product_price numeric,
product_store text
last_updated timestamp with time zone DEFAULT now()
);
INSERT into "Logs" VALUES(2095, 13, 249.99, 'BestBuy.com', '2023-04-08 00:20:12.199251+00');
INSERT into "Logs" VALUES(2096, 13, 249.99, 'Target.com', '2023-04-08 00:20:12.199251+00');
Query
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
Output
{
"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\"}}"
}
Maybe I am going about this the wrong way, I am trying to combine the rows of "Logs" that are associated with a Product ID, since this is being used in a Subquery I need this query to be in an array since there are times where there are more than one result, like above.
My desired result is:
{
"BestBuy.com": {
"data": [
[24.99, "2023-04-08 00:20:13.756603+00"],
[24.99, "2023-04-08 00:20:13.756603+00"]
]
},
"Target.com": {
"data": [
[24.99, "2023-04-08 00:20:13.756603+00"],
[24.99, "2023-04-08 00:20:13.756603+00"]
]
}
}
</details>
# 答案1
**得分**: 0
Here's the translated code:
```sql
-- 最终 JSON
with tmp as (
select json_build_object(product_store, json_build_object('Data', json_agg(t.a))) json_row from (
select
product_store, json_build_array(product_price,last_updated) a
from "Logs"
) t
group by product_store)
select json_object_agg(key, value) as result
from tmp, json_each(json_row);
Output:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{ "BestBuy.com" : {"Data" : [[249.99, "2023-04-08T00:20:12.199251+00:00"], [249.99, "2023-04-09T00:20:12.199251+00:00"], [249.99, "2023-04-10T00:20:12.199251+00:00"]]}, "Target.com" : {"Data" : [[249.99, "2023-04-08T00:20:12.199251+00:00"], [249.99, "2023-04-11T00:20:12.199251+00:00"], [249.99, "2023-04-12T00:20:12.199251+00:00"]}}
(1 row)
You can view the dbfiddle link for reference.
英文:
-- final JSON
with tmp as (
select json_build_object(product_store, json_build_object('Data', json_agg(t.a))) json_row from (
select
product_store, json_build_array(product_price,last_updated) a
from "Logs"
) t
group by product_store)
select json_object_agg(key, value) as result
from tmp, json_each(json_row);
Details:
- Inner query constructs an array from 2 columns per row using function<br>
json_build_array(product_price, last_updated)
- Function
json_agg
concatenates all the arrays into one array grouping byproduct_store
. - The
json_build_object
function builds the output json. - Finally,
json_object_agg
withjson_each
sort of concatenatejson_row
s into the final output.
Output:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{ "BestBuy.com" : {"Data" : [[249.99, "2023-04-08T00:20:12.199251+00:00"], [249.99, "2023-04-09T00:20:12.199251+00:00"], [249.99, "2023-04-10T00:20:12.199251+00:00"]]}, "Target.com" : {"Data" : [[249.99, "2023-04-08T00:20:12.199251+00:00"], [249.99, "2023-04-11T00:20:12.199251+00:00"], [249.99, "2023-04-12T00:20:12.199251+00:00"]]} }
(1 row)
See dbfiddle link.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论