• SQL 常用方法例子


    SQL left join  例子

        with a as(
            SELECT * from workspace_saiyisai.odps_base_sdkh_mid_roi
            WHERE   pt = '${s1}'
        ),
        b as(
            select _id,device_id,pt
            FROM sync_mongo_box.extract_sdkh_app_source__userinfo
            WHERE SUBSTR(create_time,1,10) = pt and pt = '${s1}'
            and _id in ("702615261", "711807701", "713043891" )       
    
        )
        SELECT  COUNT(a.device_id) AS devices
        ,round(SUM(duration)/60/COUNT(a.device_id),3) AS avg_duration
        ,round(SUM(front_end_duration)/60/COUNT(a.device_id),3) as avg_app_duration
        ,round(SUM(videos)/COUNT(a.device_id),3) as avg_videos
        ,round(SUM(coins)/COUNT(a.device_id),3) AS avg_coins
        ,ROUND(SUM(tixian_money),3) AS tixian_money
        ,round(SUM(tixian_money)/COUNT(a.device_id),3) AS avg_tixian_money
        ,ROUND(COALESCE(SUM(videos*video_price),0),3) AS ad_income
        ,round(COALESCE(SUM(videos*video_price)/COUNT(a.device_id),0),3) AS avg_ad_income
        ,ROUND(COALESCE((SUM(videos*video_price)-SUM(tixian_money)-SUM(active_cost)),0),3) AS roi
        ,round(COALESCE((SUM(videos*video_price)-SUM(tixian_money)-SUM(active_cost))/COUNT(a.device_id),0),3) AS avg_roi
        ,ROUND(COALESCE(SUM(active_cost),0),3) AS active_cost
        ,round(COALESCE(SUM(active_cost)/COUNT(a.device_id),0),3) AS avg_active_cost
        from  b LEFT  join a  on a.device_id = b.device_id and a.pt = b.pt; 

    全量ROI 模版

    select  pt, 
            CASE 
            -- 实验分桶修改 start
      
            WHEN basic_data_center.getbucketid(user_id,'${bucket_num}') BETWEEN 0 AND 9 THEN '跳一跳' 
            WHEN basic_data_center.getbucketid(user_id,'${bucket_num}') BETWEEN 10 AND 19 THEN '箭头'
            WHEN basic_data_center.getbucketid(user_id,'${bucket_num}') BETWEEN 20 AND 99 THEN '对照组'
    
            -- 实验分桶修改 end
            END AS 策略,
            count(1) as 日活,round(sum(video_count) / count(1),2) as 人均视频数,
            round(sum(times) / count(1),3) as 人均时长,round(sum(max_flash_coin + red_coin) / count(1),3) as 人均补贴,
            round(sum(income) / count(1),3) as 人均收入,ROUND(sum(possible_reward) / count(1),3) as 人均可提补贴 
            from workspace_saiyisai.odps_calc_user_roi  where pt between "${sdate}"
            and "${edate}"  
            GROUP by pt,CASE 
    
             -- 实验分桶修改 start
            WHEN basic_data_center.getbucketid(user_id,'${bucket_num}') BETWEEN 0 AND 9 THEN '跳一跳' 
            WHEN basic_data_center.getbucketid(user_id,'${bucket_num}') BETWEEN 10 AND 19 THEN '箭头'
            WHEN basic_data_center.getbucketid(user_id,'${bucket_num}') BETWEEN 20 AND 99 THEN '对照组'
             -- 实验分桶修改 end
        
            END;

    留存模版

                  with user as (
                            SELECT a.device_id
                            ,case 
                                WHEN age=1 THEN 1 else 0 end as live1
                            , case
                                WHEN age=2 THEN 1 else 0 end as live2
                            , case
                                WHEN age=3 THEN 1 else 0 end as live3
                            , case
                                WHEN age=7 THEN 1 else 0 end as live7
                            , case
                                WHEN age=14 THEN 1 else 0 end as live14
                            , case
                                WHEN age=30 THEN 1 else 0 end as live30
                            , a.pt    
                                FROM
                            (
                                SELECT device_id, pt FROM 
                            sync_mongo_box.extract_sdkh_app_source__userdaystat WHERE pt = '{partition}' {day_age}
                            ) as a 
                            LEFT JOIN 
                        (SELECT device_id, coalesce(DATEDIFF(CONCAT(pt, " 00:00:00"), CONCAT('{partition}',' 00:00:00'), 'dd'), -1) as age FROM 
                        sync_mongo_box.extract_sdkh_app_source__userdaystat    WHERE pt in (
                            SUBSTR(DATEADD(TO_DATE('{partition}','yyyy-mm-dd'),1,'dd'),1,10),
                            SUBSTR(DATEADD(TO_DATE('{partition}','yyyy-mm-dd'),2,'dd'),1,10),
                            SUBSTR(DATEADD(TO_DATE('{partition}','yyyy-mm-dd'),3,'dd'),1,10),
                            SUBSTR(DATEADD(TO_DATE('{partition}','yyyy-mm-dd'),7,'dd'),1,10),
                            SUBSTR(DATEADD(TO_DATE('{partition}','yyyy-mm-dd'),14,'dd'),1,10),
                            SUBSTR(DATEADD(TO_DATE('{partition}','yyyy-mm-dd'),30,'dd'),1,10)
                            )
                             and day_age != 0) as b
                        on a.device_id = b.device_id
                    ),
    
                    cost as (
                        SELECT cost, pt from workspace_box.base_game_data_form WHERE pt='{partition}' and app='sdkh'
                    )
    
                    SELECT
                          COUNT(DISTINCT device_id) as active
                          , round(max(cost)/count(DISTINCT device_id), 2) as one_cost
                          , sum(live1) as live1
                          , sum(live2) as live2
                          , sum(live3) as live3
                          , sum(live7) as live7
                          , sum(live14) as live14
                          , sum(live30) as live30
                    FROM user LEFT join cost 
                    on user.pt = cost.pt

    留存模版

    with user_t as(
            select pt,user_id from workspace_saiyisai.odps_calc_user_roi 
            where pt between '${date_start}' AND '${date_end}'
        ),
        userdaystat as(
            select pt,user_id from workspace_saiyisai.odps_calc_user_roi 
            where pt between '${date_start}' AND  SUBSTR(DATEADD(TO_DATE('${date_end}','yyyy-mm-dd'),14,'dd'),1,10)
        )
    select 
        CASE  
        -- 实验分桶修改 start
        WHEN basic_data_center.getbucketid(user_t.user_id,'8') BETWEEN 0 AND 5 THEN "a"
        WHEN basic_data_center.getbucketid(user_t.user_id,'8') BETWEEN 6 AND 10 THEN "b"
        WHEN basic_data_center.getbucketid(user_t.user_id,'8') BETWEEN 6 AND 10 THEN "b"
        WHEN basic_data_center.getbucketid(user_t.user_id,'8') BETWEEN 6 AND 10 THEN "b"
        -- 实验分桶修改 end
        END AS 策略,
        user_t.pt as 日期,
        count(distinct user_t.user_id) 活跃,
        sum(
            if (DATEDIFF(CONCAT(userdaystat.pt, ' 00:00:00'), CONCAT(user_t.pt, ' 00:00:00'), 'dd') = 1, 1, 0)
        ) 留存1,
        sum(
            if (DATEDIFF(CONCAT(userdaystat.pt, ' 00:00:00'), CONCAT(user_t.pt, ' 00:00:00'), 'dd') = 3, 1, 0)
        ) 留存3
    
        ROUND(
            sum(
                if (DATEDIFF(CONCAT(userdaystat.pt, ' 00:00:00'), CONCAT(user_t.pt, ' 00:00:00'), 'dd') = 1, 1, 0)
            ) /
            count(distinct user_t.user_id)
           ,3
        ) as 留存1率,
    
        ROUND(
            sum(
                if (DATEDIFF(CONCAT(userdaystat.pt, ' 00:00:00'), CONCAT(user_t.pt, ' 00:00:00'), 'dd') = 3, 1, 0)
            ) /
            count(distinct user_t.user_id)
            ,3
        ) as 留存3率
    
        from user_t LEFT JOIN userdaystat
        on user_t.user_id = userdaystat.user_id
        group by user_t.pt,
        
        -- 实验分桶修改 start
        CASE WHEN basic_data_center.getbucketid(user_t.user_id,'') BETWEEN 0 AND 99 THEN "20-99"
        
    
        -- 实验分桶修改 end
        END;
  • 相关阅读:
    spring data jpa序列化问题
    观察者模式(bilibili)
    观察者模式之Spring: event和listener
    观察者模式之Observer和Subject
    Mybatis Plus官网
    Hackinglab之注入关
    利用GitHub进行团队开发
    IIS安装Web时数据库参数配置文件写入权限验证失败
    流量分析基础篇
    Mysql启动错误1045(28000)
  • 原文地址:https://www.cnblogs.com/zhaoyingjie/p/12179271.html
Copyright © 2020-2023  润新知