当前位置:   article > 正文

SQL面试题:求品牌活动天数_求各公司营销总天数的sql题

求各公司营销总天数的sql题

题目

求各品牌活动天数,活动之间时间会有交叉重叠,重叠部分只算一次

表和数据

  1. DROP TABLE IF EXISTS test_promotion_info
  2. CREATE EXTERNAL TABLE test_promotion_info
  3. (
  4. promotion_id STRING COMMENT '优惠活动id',
  5. brand STRING COMMENT '优惠品牌',
  6. start_date STRING COMMENT '优化活动开始日期',
  7. end_date STRING COMMENT '优惠活动结束日期'
  8. ) COMMENT '各品牌活动周期表';
  9. INSERT OVERWRITE TABLE test_promotion_info VALUES
  10. (1, 'oppo', '2023-06-05', '2023-06-09'),
  11. (2, 'oppo', '2023-06-11', '2023-06-21'),
  12. (3, 'vivo', '2023-06-05', '2023-06-15'),
  13. (4, 'vivo', '2023-06-09', '2023-06-21'),
  14. (5, 'redmi', '2023-06-05', '2023-06-21'),
  15. (6, 'redmi', '2023-06-09', '2023-06-15'),
  16. (7, 'redmi', '2023-06-17', '2023-06-26'),
  17. (8, 'huawei', '2023-06-05', '2023-06-26'),
  18. (9, 'huawei', '2023-06-09', '2023-06-15'),
  19. (10, 'huawei', '2023-06-17', '2023-06-21');

解题思路

  1. 获取当前历史活动中的最大的结束日期(截止当前活动起始日期的前一天)current_max_date
  2. 处理交叉日期:
    1. current_max_date为空,说明该品牌是第一次进行活动
    2. current_max_date < start_date,说明该品牌不存在交叉
    3. current_max_date >  start_date,说明该品牌存在交叉,需要对该时间进行调整,start_date = current_max_date + 1
  3. 对brand进行 end_date - start_date + 1 求和

详细步骤

  1. WITH temp1 AS (
  2. SELECT
  3. promotion_id,
  4. brand,
  5. start_date,
  6. end_date,
  7. max(end_date) over (partition by brand order by start_date rows between unbounded preceding and 1 preceding) as current_max_date
  8. FROM
  9. test_promotion_info
  10. ), temp2 AS (
  11. SELECT
  12. promotion_id,
  13. brand,
  14. start_date,
  15. end_date,
  16. IF(
  17. current_max_date is null or current_max_date < start_date, start_date, date_add(current_max_date, 1)
  18. ) as cal_start_date
  19. FROM
  20. temp1
  21. )
  22. SELECT
  23. brand,
  24. sum(datediff(end_date, cal_start_date) + 1) AS days
  25. FROM
  26. temp2
  27. WHERE
  28. end_date > cal_start_date
  29. GROUP BY
  30. brand
  31. ;

 最终结果

  1. +---------+-------+
  2. | brand | days |
  3. +---------+-------+
  4. | oppo | 16 |
  5. | vivo | 17 |
  6. | huawei | 22 |
  7. | redmi | 22 |
  8. +---------+-------+

数据验证

  1. select
  2. promotion_id,
  3. brand,
  4. start_date,
  5. end_date,
  6. cal_start_date,
  7. IF(end_date > cal_start_date, datediff(end_date, cal_start_date) + 1, 0) as days
  8. from
  9. temp2 a;
  1. +---------------+---------+-------------+-------------+-----------------+-------+
  2. | promotion_id | brand | start_date | end_date | cal_start_date | days |
  3. +---------------+---------+-------------+-------------+-----------------+-------+
  4. | 1 | oppo | 2023-06-05 | 2023-06-09 | 2023-06-05 | 5 |
  5. | 2 | oppo | 2023-06-11 | 2023-06-21 | 2023-06-11 | 11 |
  6. | 3 | vivo | 2023-06-05 | 2023-06-15 | 2023-06-05 | 11 |
  7. | 4 | vivo | 2023-06-09 | 2023-06-21 | 2023-06-16 | 6 |
  8. | 8 | huawei | 2023-06-05 | 2023-06-26 | 2023-06-05 | 22 |
  9. | 9 | huawei | 2023-06-09 | 2023-06-15 | 2023-06-27 | 0 |
  10. | 10 | huawei | 2023-06-17 | 2023-06-21 | 2023-06-27 | 0 |
  11. | 5 | redmi | 2023-06-05 | 2023-06-21 | 2023-06-05 | 17 |
  12. | 6 | redmi | 2023-06-09 | 2023-06-15 | 2023-06-22 | 0 |
  13. | 7 | redmi | 2023-06-17 | 2023-06-26 | 2023-06-22 | 5 |
  14. +---------------+---------+-------------+-------------+-----------------+-------+

        

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/656556
推荐阅读
相关标签
  

闽ICP备14008679号