Browse Source

report_push: sql调整

Signed-off-by: Binren Zhnag <zhangbr@elab-plus.com>
Binren Zhnag 5 years ago
parent
commit
2f9cb6d11f
1 changed files with 95 additions and 81 deletions
  1. 95 81
      report_push.py

+ 95 - 81
report_push.py

@@ -20,14 +20,14 @@ class ReportPush(object):
     # 参数:时间区间 和 项目列表
     sql_1_1 = """
             SELECT SUM(pv) AS pv1 FROM a_idfa_behavior_sum
-            WHERE report_d >= %s and report_d < %s AND house_id IN %s
+            WHERE report_d >= %s and report_d <= %s AND house_id IN %s
     """
 
     # ----集团PV--权限项目范围内,求和
     # 参数:数据区间和项目列表
     sql_1_2 = """
         SELECT SUM(pv) AS pav2 FROM a_behavior_brand_mini_day
-        WHERE report_d >= %s and report_d < %s AND house_id IN %s
+        WHERE report_d >= %s and report_d <= %s AND house_id IN %s
     """
     # 2:总浏览人数
     #
@@ -46,7 +46,7 @@ class ReportPush(object):
                         a_idfa_behavior_sum A
                     LEFT JOIN d_user B ON A.user_id = B.user_id
                     WHERE
-                        A.report_d >= %s and A.report_d < %s
+                        A.report_d >= %s and A.report_d <= %s
                     AND A.house_id IN %s
                     UNION
                         SELECT
@@ -56,7 +56,7 @@ class ReportPush(object):
                             a_behavior_brand_mini_day A
                         LEFT JOIN a_brand_app_customer B ON A.brand_user_id = B.brand_customer_id
                         WHERE
-                            A.report_d >= %s and A.report_d < %s
+                            A.report_d >= %s and A.report_d <= %s
                         AND A.house_id IN %s
                 ) t1
     """
@@ -77,7 +77,7 @@ class ReportPush(object):
                         d_user
                     WHERE
                         created >= %s
-                    AND created < %s
+                    AND created <= %s
                     AND house_id IN %s
                     UNION
                         SELECT
@@ -88,7 +88,7 @@ class ReportPush(object):
                             a_brand_app_customer_house_rlat
                         WHERE
                             rlat_created >= %s
-                        AND rlat_created < %s
+                        AND rlat_created <= %s
                         AND rlat_house_id IN %s
                 ) t1
     """
@@ -107,7 +107,7 @@ class ReportPush(object):
                     d_user
                 WHERE
                     wx_phone_time >= %s
-                AND wx_phone_time < %s
+                AND wx_phone_time <= %s
                 AND house_id IN %s
                 UNION
                     SELECT
@@ -211,7 +211,7 @@ class ReportPush(object):
                         a_idfa_behavior_sum
                     WHERE
                         report_d >= %s
-                    AND report_d < %s
+                    AND report_d <= %s
                     GROUP BY
                         house_id
                 ) a
@@ -224,7 +224,7 @@ class ReportPush(object):
                     f_interested_custlist
                 WHERE
                     report_d >= %s
-                AND report_d < %s
+                AND report_d <= %s
                 GROUP BY
                     house_id
             ) c ON a.house_id = c.house_id
@@ -249,7 +249,8 @@ class ReportPush(object):
                     d_user
                 WHERE
                     created >= %s
-                AND created < %s # 时间需要加一天!!!!
+                AND created < %s=
+                	AND source IN (1, 2, 4, 10)
                 GROUP BY
                     house_id
             ) e ON a.house_id = e.house_id
@@ -262,72 +263,85 @@ class ReportPush(object):
 
     # 2.默认值/006_大麦(集团)/集团项目排行榜v1.3/集团排行榜
     sql_2_2 = """
-            select
-                a.brand_id,
-                a.house_id,
-                a.house_name,
-                ifnull(SUM(a.pv), 0),
-                SUM(a.uv),
-                SUM(a.new_cust),
-                SUM(a.shouquan_cust)
-            from           
-            (SELECT
-                            c.pv,c.uv,a.brand_id,a.house_id, a.house_name,a.brand_name,
-                            ifnull(b.house_layout_num,0) house_layout_num,
-                                ifnull(d.launch_time,'--') launch_time,
-                                c.new_cust,
-                                c.shouquan_cust,
-                                c.revisit_cust
-                        FROM
+                SELECT
+                        a.house_id,
+                        a.house_name,
+                        ifnull(SUM(a.pv), 0),
+                        SUM(a.uv),
+                        SUM(a.new_cust_num),
+                        SUM(a.wx_num)
+                        from
                             (
-                            SELECT
-                                brand_id,
-                                ifnull(house_id, '0') house_id,
-                                sum(pv) pv,
-                                count(DISTINCT brand_user_id) uv,
-                                        count(DISTINCT case when is_new_user = 1 then 
-                                            brand_user_id end) new_cust,
-                                        count(DISTINCT case when is_shouquan_user = 1 then 
-                                            brand_user_id end) shouquan_cust,
-                                        count(DISTINCT case when is_new_user = 0 then 
-                                            brand_user_id end) revisit_cust
-                         from a_behavior_brand_mini_day
-                            where report_d >= %s
-                                        and report_d < %s
-                                GROUP BY
-                                brand_id,
-                                ifnull(house_id, '0')
-                        ) c
-                        LEFT JOIN (
-                            SELECT
-                                house_id,
-                                count(1) house_layout_num
-                            FROM
-                                d_content_layout
-                            WHERE
-                                status = '1'
-                                 and house_id <> 1
-                        group by house_id
-                        union all
-                            SELECT
-                                bb.brand_id house_id,
-                                count(1) house_layout_num
-                            FROM
-                                d_content_layout aa join d_house bb
-                            on aa.house_id = bb.house_id
-                            WHERE
-                                aa.status = '1'
-                                        and bb.status = '1'
-                                        and aa.house_id <> 1
-                        group by bb.brand_id
-                        ) b ON c.house_id = b.house_id
-                         JOIN d_house a ON a.house_id = c.house_id
-                        and a.brand_id = c.brand_id
-                        left join d_house_attr d
-                         on c.house_id = d.house_id
-                        and c.brand_id = d.brand_id ) a 
-            group by a.brand_id, a.house_id, a.house_name
-            order by a.pv desc
+                SELECT
+                    a.*, b.house_name,
+                    c.interested_num,
+                    d.wx_num,
+                    e.new_cust_num
+                FROM
+                    (
+                        SELECT
+                            house_id,
+                            count(
+                                DISTINCT ifnull(user_id, idfa)
+                            ) uv,
+                            sum(session_times) session_times,
+                            sum(sum_session_time) sum_session_time,
+                            sum(pv) pv,
+                            sum(page_num) page_num
+                        FROM
+                            a_idfa_behavior_sum
+                        WHERE
+                            report_d >= %s
+                        AND report_d <= %s
+                        GROUP BY
+                            house_id
+                    ) a
+                JOIN d_house b ON a.house_id = b.house_id
+                LEFT JOIN (
+                    SELECT
+                        house_id,
+                        count(DISTINCT customer_id) interested_num
+                    FROM
+                        f_interested_custlist
+                    WHERE
+                        report_d >=%s
+                    AND report_d <=%s
+                    GROUP BY
+                        house_id
+                ) c ON a.house_id = c.house_id
+                LEFT JOIN (
+                    SELECT
+                        house_id,
+                        count(DISTINCT mobile) wx_num
+                    FROM
+                        f_customer_dynamic
+                    WHERE
+                        dynamic IN (1, 2, 4)
+                    AND report_d >=%s
+                    AND report_d <=%s
+                    GROUP BY
+                        house_id
+                ) d ON a.house_id = d.house_id
+                LEFT JOIN (
+                    SELECT
+                        house_id,
+                        count(DISTINCT user_id) new_cust_num
+                    FROM
+                        d_user
+                    WHERE
+                        source IN (1, 2, 3, 4, 10)
+                    AND created >=%s
+                    AND created < DATE_ADD(
+                        %s, INTERVAL 1 DAY
+                    )
+                    GROUP BY
+                        house_id
+                ) e ON a.house_id = e.house_id)
+                a
+                        GROUP BY
+                            a.house_id,
+                            a.house_name
+                        order by a.pv desc
     """
 
     # 默认值/001_大麦/场景_用户来源渠道/用户来源渠道—明细
@@ -343,7 +357,7 @@ class ReportPush(object):
             WHERE
                 a.source IN (1, 2, 3, 4, 10)
             AND a.report_d >= %s
-            AND a.report_d < %s
+            AND a.report_d <= %s
             GROUP BY
                 house_id,
                 house_name,
@@ -794,13 +808,13 @@ class ReportPush(object):
             for index, x in enumerate(xcx_top_data):
                 if str(house_id) == str(x[0]):
                     a.extend(x)
-                    a_order = index
+                    a_order = index + 1
             b = []
             b_order = 0
             for index, y in enumerate(brand_top_data):
                 if str(house_id) == str(y[1]):
                     b.extend(y)
-                    b_order = index
+                    b_order = index + 1
 
             order = b_order if b_order > 0 else a_order
             if len(a) > 0 and len(b) > 0:
@@ -1046,10 +1060,12 @@ class ReportPush(object):
         if task_key in (2, 3):
             # 上周,上周一到上周天
             pre_time = now_time + datetime.timedelta(days=-7)
+            now_time = now_time + datetime.timedelta(days=-1)
             pass
         elif task_key in (1, 1):
             # 昨天
             pre_time = now_time + datetime.timedelta(days=-1)
+            now_time = now_time + datetime.timedelta(days=-1)
             pass
         elif task_key in (4, 4):
             # 不限时间
@@ -1059,6 +1075,4 @@ class ReportPush(object):
 
 if __name__ == '__main__':
     rp = ReportPush('linshi')
-    sql = "select house_id from mvp_crowd_info limit 10"
-    for x in rp.db.select(sql):
-        print(x)
+    print(rp.get_time_range(2))