gpt4 book ai didi

sql - 创建 JSON 和编辑复杂查询 (oracle 11g)

转载 作者:行者123 更新时间:2023-12-04 01:31:26 26 4
gpt4 key购买 nike

我有 4 个不同的表:

table_price_product (contains information related to the products and their price)

table_price_list (contains the information related to the price list)

prices_per_client (contains the information related to the prices for the different clients given a specific product)

clients (contains the information related to the clients)

这是我的 SQL FIDDLE:LINK

我明白了:

CUSTOMER_NUMBER  |  CUSTOMER_CLASS_CODE|    PRICE
(null) | A | 29223
(null) | B | 33223
112121 | E | 40340
119435 | E | 40340

现在,我想在一次查询中获取与产品和客户相关的所有数据,将 A、B、C 等分别替换为 CLASS A、CLASS B、CLASS C 但是如果customer_class_code 是 = 'E' 我想从表 clients 中获取名称,最后,转换所有内容并获取每个产品的 JSON。看看这个,不知何故我需要添加列“PRICES_FOR_CLIENTS”和“groups”,因为我需要这些列来生成 JSON。

SKU     |PRICE|PRICES_FOR_CLIENTS|groups|CLASS A|CLASS B|WALMART|SUPERMARKET
99342435|9999 | | |29223 |33223 |40340 |40340

image

我想用该信息生成一个 JSON:

{"sku":"99342435","PRICE":"9999",PRICES_FOR_CLIENTS:[{"group":"CLASS A", "PRICE":"29223"},{"group":"CLASS B", "PRICE":"33223"},{"group":"WALMART","PRICE":"40340"},{"group":"SUPERMARKET", "PRICE":"40340"}]};

你能帮帮我吗?

编辑:

<Item SKU="99342435" Price="9999">
<PRICES_FOR_CLIENTS>
<CLIENT_PRICE>
<Client>WALMART</Client>
<Price>40340</Price>
<Site>USSITE</Site>
</CLIENT_PRICE>
<CLIENT_PRICE>
<Client>SUPERMARKET</Client>
<Price>48343</Price>
<Site>USSITE</Site>
</CLIENT_PRICE>
<CLIENT_PRICE>
<Client>B</Client>
<Price>33223</Price>
<Site>USSITE</Site>
</CLIENT_PRICE>
<CLIENT_PRICE>
<Client>A</Client>
<Price>29223</Price>
<Site>USSITE</Site>
</CLIENT_PRICE>
</PRICES_FOR_CLIENTS>
</Item>

最佳答案

我认为您可以将大部分代码替换为以下查询。您可能需要调整 IN 子句,如果您经常更改客户列表,这会很痛苦。但这会复制您的结果:

SELECT *
FROM (SELECT DECODE(ppc.customer_class_code, 'E', c.description, ppc.customer_class_code) AS IDENTIFIER, tpp.item_code, tpp.price AS ITEM_PRICE, ppc.price
FROM table_price_list tpl
INNER JOIN table_price_product tpp ON tpp.list_header_id = tpl.list_header_id AND tpp.request_id = tpl.request_id
INNER JOIN prices_per_client ppc ON tpp.item_code = ppc.item_code
LEFT JOIN clients c ON ppc.customer_number = c.account_number
WHERE SYSDATE BETWEEN NVL(tpp.start_date_active, SYSDATE) AND NVL(tpp.end_date_active, SYSDATE+1))
PIVOT (AVG(PRICE) FOR IDENTIFIER IN ('A' AS CLASS_A , 'B' AS CLASS_B, 'SUPERMARKET' AS SUPERMARKET, 'WALMART' AS WALMART));

这是更新 fiddle .

至于 JSON 输出,如果您使用的是更高版本,它会容易得多,因为它现在是核心功能的一部分。

编辑:为每个评论添加 XML 功能

你可以检查这个查询:

SELECT XMLSERIALIZE(CONTENT
XMLELEMENT("Item",
XMLATTRIBUTES(sub.item_code AS "SKU", sub.item_price AS "Price"),
XMLELEMENT("PRICES_FOR_CLIENTS",
XMLAGG(XMLELEMENT("CLIENT_PRICE",
XMLFOREST(sub.identifier AS "Client", sub.price AS "Price"))))) AS CLOB INDENT)
FROM (SELECT DECODE(ppc.customer_class_code, 'E', c.description, ppc.customer_class_code) AS IDENTIFIER, tpp.item_code, tpp.price AS ITEM_PRICE, avg(ppc.price) AS PRICE
FROM table_price_list tpl
INNER JOIN table_price_product tpp ON tpp.list_header_id = tpl.list_header_id AND tpp.request_id = tpl.request_id
INNER JOIN prices_per_client ppc ON tpp.item_code = ppc.item_code
LEFT JOIN clients c ON ppc.customer_number = c.account_number
WHERE SYSDATE BETWEEN NVL(tpp.start_date_active, SYSDATE) AND NVL(tpp.end_date_active, SYSDATE+1)
GROUP BY DECODE(ppc.customer_class_code, 'E', c.description, ppc.customer_class_code), tpp.item_code, tpp.price) sub
WHERE sub.identifier IS NOT NULL
GROUP BY sub.item_code, sub.item_price;

这是该查询的更新 fiddle (Link)。

产生以下输出:

<Item SKU="99342435" Price="9999">
<PRICES_FOR_CLIENTS>
<CLIENT_PRICE>
<Client>WALMART</Client>
<Price>40340</Price>
</CLIENT_PRICE>
<CLIENT_PRICE>
<Client>SUPERMARKET</Client>
<Price>48343</Price>
</CLIENT_PRICE>
<CLIENT_PRICE>
<Client>B</Client>
<Price>33223</Price>
</CLIENT_PRICE>
<CLIENT_PRICE>
<Client>A</Client>
<Price>29223</Price>
</CLIENT_PRICE>
</PRICES_FOR_CLIENTS>
</Item>

编辑 2:通过字符串连接添加 JSON

以下将通过直接字符串连接输出 JSON:

SELECT '{"sku":"'||sub.item_code||'","PRICE":"'||sub.item_price||'",PRICES_FOR_CLIENTS:['||listagg('{"group":"'||sub.identifier||'","PRICE":"'||sub.price||'"}',',') WITHIN GROUP (ORDER BY sub.identifier)||']};' AS JSON                                              
FROM (SELECT DECODE(ppc.customer_class_code, 'E', c.description, ppc.customer_class_code) AS IDENTIFIER, tpp.item_code, replace(tpp.price, ',', '.') AS ITEM_PRICE, REPLACE(avg(ppc.price), ',', '.') AS PRICE,
tpl.request_id, max(tpl.request_id) over (partition by tpp.item_code) as max_request
FROM table_price_list tpl
INNER JOIN table_price_product tpp ON tpp.list_header_id = tpl.list_header_id AND tpp.request_id = tpl.request_id
INNER JOIN prices_per_client ppc ON tpp.item_code = ppc.item_code
LEFT JOIN clients c ON ppc.customer_number = c.account_number
WHERE SYSDATE BETWEEN NVL(tpp.start_date_active, SYSDATE) AND NVL(tpp.end_date_active, SYSDATE+1)
GROUP BY DECODE(ppc.customer_class_code, 'E', c.description, ppc.customer_class_code), tpp.item_code, tpp.price, tpl.request_id) sub
WHERE sub.identifier IS NOT NULL
and sub.request_id = sub.max_request
GROUP BY sub.item_code, sub.item_price;

以及此查询的更新 fiddle (Link)

编辑 3:添加替换**编辑 4:添加分析功能 **

关于sql - 创建 JSON 和编辑复杂查询 (oracle 11g),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60940909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com