创建 JSON 并编辑复杂查询 (oracle 11g)
Creating JSON and editing a complex query (oracle 11g)
我有 4 个不同的 tables:
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' 我想从 table clients 最后,转换所有内容并为每个产品获得 JSONs。看看这个,不知何故我需要添加列 "PRICES_FOR_CLIENTS" 和 "groups" 因为我需要这些列来生成 JSON.
SKU |PRICE|PRICES_FOR_CLIENTS|groups|CLASS A|CLASS B|WALMART|SUPERMARKET
99342435|9999 | | |29223 |33223 |40340 |40340
我想用这些信息生成一个 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:添加了分析功能 **
我有 4 个不同的 tables:
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' 我想从 table clients 最后,转换所有内容并为每个产品获得 JSONs。看看这个,不知何故我需要添加列 "PRICES_FOR_CLIENTS" 和 "groups" 因为我需要这些列来生成 JSON.
SKU |PRICE|PRICES_FOR_CLIENTS|groups|CLASS A|CLASS B|WALMART|SUPERMARKET
99342435|9999 | | |29223 |33223 |40340 |40340
我想用这些信息生成一个 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:添加了分析功能 **