-- ======================================
-- 所属报表:品项主要指标达成
-- ======================================


WITH data_1 AS (
    SELECT
        t2.pl_name AS "产品线",
        TO_CHAR( t1.stat_date, 'YYYY' ) AS "年",
        TO_CHAR( t1.stat_date, 'YYYY-MM' ) AS "年月",
        TO_CHAR( t1.stat_date, 'Q' ) AS "季",
        TO_CHAR( t1.stat_date, 'YYYY-MM-DD' ) AS "日",
        t2.pi_name AS "品项",
        t2.pr_name AS "产品系列",
        t2.product_code AS "产品编码",
        t2.product_name AS "产品名称",
        t2.spec AS "规格",
        t2.pack_base_num AS "转换率",
        SUM(CASE WHEN t1.indi_type IN ('发货','电商订单','费用发货') THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "发货",
        SUM(CASE WHEN t1.indi_type IN ('出货','电商订单') THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "出货",
        SUM(CASE WHEN t1.indi_type = '合同' THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "合同",
        SUM(CASE WHEN t1.indi_type = '实物库存' THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "实物库存",
        SUM(CASE WHEN t1.indi_type = '在途库存' THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "在途库存",
        SUM(CASE WHEN t1.indi_type = '未发货' THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "未发货",
        SUM(CASE WHEN t1.indi_type IN('订单','电商订单','费用订单') THEN ${SWITCH(sLvl,0,'t1.amt_bu',1,'t1.amt_region',2,'t1.amt_office',3,'t1.amt_city_unit')} ELSE 0 END) AS "订单"
    FROM
        dm.dm_sal_indicator_day	t1
            LEFT JOIN dwr.dim_product t2 ON t1.product_code = t2.product_code
    WHERE
        t1.indi_type IN ('发货','电商订单','费用发货','出货','合同','实物库存','在途库存','未发货','订单','费用订单')
      AND t1.stat_date BETWEEN '${FORMAT(YEARDELTA(sEnd_mon,-1),"yyyy-01-01")}' AND '${sEnd_mon}'
      AND EXISTS (
        SELECT
        1
	FROM
		dm.dm_bi_user_permisson
	WHERE
		bi_user = '${fine_username}'
		AND per_model IN ('all','sal')
		AND (
				is_all = 1
				OR per_code = t1.bu_code
				OR per_code = t1.region_code
				OR per_code = t1.office_code
				OR per_code = t1.city_unit_code
				)
	)
        ${IF(LEN(sBu)==0,"","AND t1.bu_code IN ('"+sBu+"')")}
            ${IF(LEN(sRegion)==0,"","AND t1.region_code IN ('"+sRegion+"')")}
            ${IF(LEN(sOffice)==0,"","AND t1.office_code IN ('"+sOffice+"')")}
            ${IF(LEN(sCu)==0,"","AND t1.city_unit_code IN ('"+sCu+"')")}
            ${IF(LEN(sProvince)==0,"","AND t1.province_name IN ('"+sProvince+"')")}
            ${IF(LEN(sProd)==0,"","AND t1.product_code IN ('"+sProd+"')")}
            ${IF(LEN(sPi_name)==0,"","AND t2.pi_name IN ('"+sPi_name+"')")}
    GROUP BY
        "产品线",
        "年",
        "年月",
        "季",
        "日",
        "品项",
        "产品系列",
        "产品编码",
        "产品名称",
        "规格",
        "转换率"
)

SELECT
    "产品线",
    "品项",
    "产品系列",
    "产品编码",
    "产品名称",
    "规格",
    "转换率",
    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' AND "日" <= '${sEnd_mon}' THEN "发货" ELSE 0 END) / 10000 AS "今年累计_发货",
    SUM(CASE WHEN "年月" <= '${FORMAT(YEARDELTA(sEnd_mon,-1),"yyyy-MM")}' THEN "发货" ELSE 0 END) / 10000 AS "去年同期_发货",
    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' AND "季" = QUARTER(DATE('${sEnd_mon}')) AND "日" <= '${sEnd_mon}' THEN "发货" ELSE 0 END) / 10000 AS "本季累计_发货",
    SUM(CASE WHEN "年月" = '${FORMAT(sEnd_mon,"yyyy-MM")}' AND "日" <= '${sEnd_mon}' THEN "发货" ELSE 0 END) / 10000 AS "今年当月累计_发货",

    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' AND "日" <= '${sEnd_mon}' THEN "订单" ELSE 0 END) / 10000 AS "今年累计_订单",
    SUM(CASE WHEN "年月" <= '${FORMAT(YEARDELTA(sEnd_mon,-1),"yyyy-MM")}' THEN "订单" ELSE 0 END) / 10000 AS "去年同期_订单",
    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' AND "季" = QUARTER(DATE('${sEnd_mon}')) AND "日" <= '${sEnd_mon}' THEN "订单" ELSE 0 END) / 10000 AS "本季累计_订单",
    SUM(CASE WHEN "年月" = '${FORMAT(sEnd_mon,"yyyy-MM")}' AND "日" <= '${sEnd_mon}' THEN "订单" ELSE 0 END) / 10000 AS "今年当月累计_订单",

    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' AND "日" <= '${sEnd_mon}' THEN "出货" ELSE 0 END) / 10000 AS "今年累计_出货",
    SUM(CASE WHEN "年月" <= '${FORMAT(YEARDELTA(sEnd_mon,-1),"yyyy-MM")}' THEN "出货" ELSE 0 END) / 10000 AS "去年同期_出货",
    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' AND "季" = QUARTER(DATE('${sEnd_mon}')) AND "日" <= '${sEnd_mon}' THEN "出货" ELSE 0 END) / 10000 AS "本季累计_出货",
    SUM(CASE WHEN "年月" = '${FORMAT(sEnd_mon,"yyyy-MM")}' AND "日" <= '${sEnd_mon}' THEN "出货" ELSE 0 END) / 10000 AS "今年当月累计_出货",

    SUM(CASE WHEN "年" = '${YEAR(sEnd_mon)}' THEN "合同" ELSE 0 END) / 10000 AS "今年累计_合同",
    SUM(CASE WHEN "年月" = '${FORMAT(sEnd_mon,"yyyy-MM")}' THEN "实物库存" ELSE 0 END) / 10000 AS "当月末经销商库存_实物库存",
    SUM(CASE WHEN "年月" = '${FORMAT(sEnd_mon,"yyyy-MM")}' THEN "在途库存" ELSE 0 END) / 10000 AS "当月末经销商库存_在途",
    SUM(CASE WHEN "年月" = '${FORMAT(sEnd_mon,"yyyy-MM")}' THEN "未发货" ELSE 0 END) / 10000 AS "当月末经销商库存_未发货"





FROM
    data_1

WHERE
    "产品线" IS NOT NULL
  AND "品项" IS NOT NULL
GROUP BY
    "产品线",
    "品项",
    "产品系列",
    "产品编码",
    "产品名称",
    "规格",
    "转换率"
ORDER BY
    "产品线",
    "品项",
    "产品系列",
    "产品编码"