set search_path = 'bi_dashboard';
WITH F_SRV_DB_DIM_PRD_D AS (SELECT EXTERNAL_NAME FROM ( SELECT MKT_NAME EXTERNAL_NAME
FROM BI_DASHBOARD.DM_MSS_ITEM_PRODUCT_D PRD
WHERE PRD.COMPANY_BRAND =any(array[string_to_array('HUAWEI',',')])
AND PRD.MKT_NAME =any(array[string_to_array('畅享 60,畅享 50,畅享 60X,畅享 60 Pro,畅享 50 Pro,畅享 50z,nova 10z,畅享 20e,畅享20 Pro,畅享 10e,畅享10 Plus,畅享20 SE,畅享10,nova 11i,畅享20 Plus,畅享9 Plus,畅享20 5G,nova Y90,畅享 10S,nova Y70,畅享Z,畅享 9S,nova 8 SE 活力版,麦芒9 5G,Y9s,麦芒9 5G',',')])
) WHERE EXTERNAL_NAME<>'SNULL' GROUP BY EXTERNAL_NAME),
V_PERIOD AS
(
SELECT PERIOD_ID AS PERIOD_ID_M,
LEAST(TO_CHAR(PERIOD_END_DATE, 'YYYYMMDD'), '20230630') AS PERIOD_ID,
PERIOD_ID AS DATES
FROM BI_DASHBOARD.RPT_TML_ACCOUNT_PERIOD_D
WHERE PERIOD_TYPE = 'M'
AND PERIOD_ID BETWEEN 202207 AND 202306
),
V_DATA_BASE AS
(
SELECT A.PERIOD_ID,
IFNULL(A.CHANNEL_NAME, 'SNULL') AS DISTRIBUTOR_CHANNEL_NAME,
SUM(A.SO_QTY_MTD) AS SO_QTY,
SUM(DECODE(A.PERIOD_ID, 20230630, A.SO_QTY_MTD)) AS SO_QTY_ORDER
select count(*) FROM DM_MSS_CN_PC_REP_RP_ST_D_F A
INNER JOIN F_SRV_DB_DIM_PRD_D PRD
ON A.EXTERNAL_NAME = PRD.EXTERNAL_NAME
WHERE 1 = 1
AND A.CHANNEL_ID IN ('100013388802')
AND A.ORG_KEY IN (10000651)
AND A.SALES_FLAG IN ('1', '0')
AND A.PERIOD_ID IN (20220731,20221031,20220930,20220831,20221130,20221231,20230131,20230228,20230430,20230331,20230531,20230630)
AND (A.SO_QTY_MTD <> 0) -- 过滤所有日期SO_QTY为0的数据
GROUP BY A.PERIOD_ID,
IFNULL(A.CHANNEL_NAME, 'SNULL')
),
V_DATA AS
(
SELECT PERIOD_ID,
NVL(DISTRIBUTOR_CHANNEL_NAME, 'Total') AS DISTRIBUTOR_CHANNEL_NAME,
SUM(SO_QTY) AS SO_QTY,
SUM(SO_QTY_ORDER) AS SO_QTY_ORDER
FROM V_DATA_BASE A
GROUP BY GROUPING SETS ((PERIOD_ID), (PERIOD_ID, DISTRIBUTOR_CHANNEL_NAME))
)
SELECT STRING_AGG(P.DATES, ',' ORDER BY P.PERIOD_ID_M) AS PERIOD_LIST,
B.DISTRIBUTOR_CHANNEL_NAME,
STRING_AGG(NVL(TO_CHAR(ROUND(A.SO_QTY)), '0'), ',' ORDER BY P.PERIOD_ID_M) AS SO_QTY
FROM V_PERIOD P
FULL JOIN (SELECT DISTINCT DISTRIBUTOR_CHANNEL_NAME FROM V_DATA) B
ON 1 = 1
LEFT JOIN V_DATA A
ON A.PERIOD_ID = P.PERIOD_ID
AND A.DISTRIBUTOR_CHANNEL_NAME = B.DISTRIBUTOR_CHANNEL_NAME
GROUP BY B.DISTRIBUTOR_CHANNEL_NAME
ORDER BY DECODE(B.DISTRIBUTOR_CHANNEL_NAME, 'Total', 0, 'SOURCE IS NULL', 2, '源为空', 3, 'SNULL', 4, 1),
SUM(A.SO_QTY_ORDER) DESC NULLS LAST
LIMIT 50 OFFSET 0
评论