WITH t_base AS 
(
    SELECT  dt
            ,hh
            ,apptype 
            -- ,CASE   WHEN apptype IN ("4") AND abcode IN ("ab0","ab1") THEN "实验组-先验地域降权"
            --         WHEN apptype IN ("4") AND abcode IN ("ab4","ab5","ab6","ab7","ab8","ab9") THEN "实验组-str+校准"
            --         WHEN apptype IN ("4") AND abcode IN ("ab2","ab3") THEN "对照组"
            --         WHEN apptype IN ("0") AND abcode IN ("ab0","ab1","ab4","ab5","ab6","ab7","ab8","ab9") THEN "实验组-str+校准"
            --         WHEN apptype IN ("0") AND abcode IN ("ab2","ab3") THEN "对照组"
            --         ELSE "其他"
            -- END AS abcode
            ,CASE   WHEN apptype IN ("4") AND abcode IN ("ab0") THEN "实验组-先验地域降权"
                    WHEN apptype IN ("4") AND abcode IN ("ab6") THEN "实验组-str+校准&ros-统计量"
                    WHEN apptype IN ("4") AND abcode IN ("ab8") THEN "实验组-str+校准&ros损失函数优化"
                    WHEN apptype IN ("4") AND abcode IN ("ab4") THEN "实验组-str+校准&ros天级更新"
                    WHEN apptype IN ("4") AND abcode IN ("ab2") THEN "对照组"
                    ELSE "其他"
            END AS abcode
            -- ,CASE   WHEN apptype IN ("4") AND abcode IN ("ab0","ab1") THEN "实验组-先验地域降权"
            --         WHEN apptype IN ("4") AND abcode IN ("ab8","ab9") THEN "实验组-str+校准"
            --         WHEN apptype IN ("4") AND abcode IN ("ab2","ab3","ab4","ab5","ab6","ab7") THEN "对照组"
            --         ELSE "其他"
            -- END AS abcode
            ,CASE   WHEN page IN ("回流后沉浸页&内页feed","详情后沉浸页","首页feed","详情页") THEN "推荐"
                    WHEN page IN ("回流页","其他") THEN "非推荐"
                    ELSE "其他"
            END AS page
            ,mid
            ,vid
            ,is_share
            ,share_cnt
            ,is_return_1
            ,is_return_n
            ,is_return_noself
            ,return_1_uv
            ,return_n_uv
            ,return_n_uv_noself
            ,new_exposure_cnt
            ,flowpool
    FROM    loghubods.dwd_recsys_alg_exposure_base_20250108
    WHERE   dt = '${dt}'
--     AND     hh BETWEEN "16" AND "24"
    AND     apptype IN ("4")
    AND     page IN ("回流后沉浸页&内页feed","详情后沉浸页","首页feed","详情页","回流页","其他")
    AND     abcode IN ("ab0","ab1","ab2","ab3","ab4","ab5","ab6","ab7","ab8","ab9")
    AND     abcode NOT IN ("ab100")
)
SELECT  dt
        ,COALESCE(apptype,"sum") AS apptype
        ,COALESCE(abcode,"sum") AS abcode
        ,COALESCE(page,"sum") AS page
        ,round(COALESCE(COUNT(1) / COUNT(DISTINCT mid),0),2) AS exp_per_dau
        ,round(COALESCE(SUM(is_share) / COUNT(1),0),6) AS str_one
        ,round(COALESCE(SUM(return_n_uv) / SUM(is_share),0),6) AS ros_one
        ,round(COALESCE(SUM(share_cnt) / COUNT(1),0),6) AS str
        ,round(COALESCE(SUM(return_n_uv) / SUM(share_cnt),0),6) AS ros
        ,round(COALESCE(SUM(is_return_1) / COUNT(1),0),6) AS str_plus
        ,round(COALESCE(SUM(return_n_uv) / SUM(is_return_1),0),6) AS ros_minus
        ,round(COALESCE(SUM(return_n_uv) / COUNT(1),0),6) AS rovn
        ,round(COALESCE(SUM(new_exposure_cnt) / COUNT(1),0),6) AS vovh24
        ,COUNT(DISTINCT mid) AS dau
        ,COUNT(1) AS exp
        ,COALESCE(SUM(is_share),0) AS is_share
        ,COALESCE(SUM(share_cnt),0) AS share_cnt
        ,COALESCE(SUM(is_return_1),0) AS is_return_1
        ,COALESCE(SUM(return_n_uv),0) AS return_n_uv
        ,COALESCE(SUM(new_exposure_cnt),0) AS viewh24
        ,COALESCE(SUM(return_n_uv_noself),0) AS return_n_uv_noself -- ,MAX(CAST(COALESCE(share_cnt,'0') AS BIGINT)) AS max_share_cnt
        -- ,MAX(CAST(COALESCE(return_1_uv,'0') AS BIGINT)) AS max_return_1_uv
        -- ,MAX(CAST(COALESCE(return_n_uv,'0') AS BIGINT)) AS max_return_n_uv
        -- ,MAX(CAST(COALESCE(return_n_uv_noself,'0') AS BIGINT)) AS max_return_n_uv_noself
        -- ,COALESCE(SUM(is_return_noself),0) AS is_return_noself
        -- ,COALESCE(SUM(return_1_uv),0) AS return_1_uv
        -- ,COUNT(DISTINCT vid) AS exp_vid_cnt
        -- ,COUNT(DISTINCT CASE    WHEN is_share = '1' THEN vid ELSE NULL END) AS share_vid_cnt
        -- ,COUNT(DISTINCT CASE    WHEN is_return_n = '1' THEN vid ELSE NULL END) AS return_vid_cnt
        ,min(hh) as start_hh
        ,max(hh) as end_hh
FROM    t_base
where page in ("推荐")
GROUP BY dt
         ,apptype
         ,abcode
         ,page
-- GROUPING SETS ((dt,apptype,abcode)
--               ,(dt,apptype,abcode,page))
ORDER BY dt DESC,apptype,page,abcode
;