小编典典

我认为您可以使用以下查询替换大多数代码。您可能需要调整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));

这是一个更新小提琴。

至于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;

这是该查询的更新小提琴(Link)。

产生以下输出:

WALMART

40340

SUPERMARKET

48343

B

33223

A

29223

编辑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;

以及与此查询相关的更新小提琴(Link)

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

2021-03-08

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐