HiveSQL题——array_contains函数

news2025/2/19 23:22:48

目录

一、原创文章被引用次数

0 问题描述

1 数据准备

2 数据分析

​编辑

3 小结

二、学生退费人数

0 问题描述

1 数据准备

2 数据分析

3 小结

一、原创文章被引用次数

0 问题描述

   求原创文章被引用的次数,注意本题不能用关联的形式求解。

1 数据准备

   id表示文章id,oid表示引用的其他文章id,当oid为0时表示当前文章为原创文章。

create table if not exists  table18
(
    id    int comment '文章id',
    oid   int comment '引用的其他文章id'
) comment '文章信息表';

insert overwrite table table18 values
(1,0),
(2,0),
(3,1),
(4,1),
(5,2),
(6,0),
(7,3);

2 数据分析

    题目要求的是原创文章被引用的次数,其中原创文章为oid等于0的文章,即求解文章id为【1,2,6】被引用的次数。常见的思路是用关联方式求解,具体SQL如下图所示:

思路一:用左连接 left join 


--思路一:用左连接 left join 
select
    t1.id,
    count(t2.oid) as cnt
from (select * from table18 where oid = 0) t1
         left join
         (select * from table18 where oid <> 0) t2
         on t1.id = t2.oid
group by t1.id
order by t1.id;

 输出结果为:

 题意要求不能使用join等关联形式求解,其实该题本质是存在性计数问题

思路二:借助array_contains(array,element) 函数

select
    new_id,
    sum(flag)as cnt
from (
         select
             id,
             oid,
             contains,
             -- 第二步:利用array_contains()函数判断引用的oid是否在原创文章id集合中,ture则记为1,false则记为0
             if(array_contains(contains, oid), 1, 0)    flag,
             -- 第三步:清洗数据,补充完整的原创文章
             if(array_contains(contains, oid), oid, if(oid = 0, id, null)) new_id
         from ( -- 第一步:构建原创文章id集合,作为辅助列
                  select
                      id,
                      oid,
                      collect_set(if(oid = 0, id, null)) over () contains
                  from table18
              ) tmp1
     ) tmp2
where new_id is not null
group by new_id;

    上述代码解析:通过array_contains(array,column) 函数进行存在性检测,如果array中包含column 则记为1,不存在记为0,关键公式: sum(if(array_contains(array,column),1,0))

上述代码解析:

第一步:构建原创文章id集合contains,将contains作为辅助列。

  select
        id,
        oid,
        collect_set(if(oid = 0, id, null)) over () contains
from table18;

第二步:利用array_contains()函数,判断非原创的oid是否在原创文章id集合中,存在则计数为1,否则计数为0。

select
        id,
        oid,
        contains,
        if(array_contains(contains, oid), 1, 0) as flag
from ( 
         select
               id,
               oid,
               collect_set(if(oid = 0, id, null)) over () contains
          from table18
      ) tmp1;

第三步:清洗数据,对原创文章id补充完整

select
        id,
        oid,
        contains,
        if(array_contains(contains, oid), 1, 0)   flag,
      --清洗数据,对原创文章id补充完整
        if(array_contains(contains, oid), oid, if(oid = 0, id, null)) new_id
from ( 
          select
               id,
               oid,
               collect_set(if(oid = 0, id, null)) over () contains
          from table18
       ) tmp1;

 

   ps: 此处需要对原创文章id补充完整,否则会丢失记录。具体是:通过array_contains(contains,oid)去判断,代码为 if(array_contains(contains, oid), oid, if(oid = 0, id, null)) as  new_id   -->  代表的意思是如果oid存在于原创文章id构建的集合中,就取得该oid,如果不存在,再判断oid是否为0,如果是0,则取得id,否则记为null。

第四步:将new_id 为null的数据滤掉,并对new_id分组,求出各原创文章被引用的次数sum(flag)as cnt

select
    new_id,
    sum(flag)as cnt
from (
         select
             id,
             oid,
             contains,
             -- 第二步:利用array_contains()函数判断引用的oid是否在原创文章id集合中,ture则记为1,false则记为0
             if(array_contains(contains, oid), 1, 0)    flag,
             -- 第三步:清洗数据,补充完整的原创文章id
             if(array_contains(contains, oid), oid, if(oid = 0, id, null)) new_id
         from ( -- 第一步:构建原创文章id集合,作为辅助列
                  select
                      id,
                      oid,
                      collect_set(if(oid = 0, id, null)) over () contains
                  from table18
              ) tmp1
     ) tmp2
  -- 第四步:将为null的数值过滤掉,并对new_id分组,求出各原创文章被引用的次数sum(flag)as cnt
where new_id is not null
group by new_id;

3 小结

 上述例子中利用array_contains(array,column)进行存在性检测,如果存在则记为1,不存在则记为0,核心计算公式为 sum(if(array_contains(array,value),1,0))

二、学生退费人数

0 问题描述

求截止当前月的学生退费总人数【当月的学生退费人数:上月存在,这月不存在的学生个数】。

1 数据准备

create table if not exists test19( dt string comment '日期',
stu_id string comment '学生id');

insert overwrite table test19
values ('2020-01-02','1001'),
       ('2020-01-02','1002'),
       ('2020-02-02','1001'),
       ('2020-02-02','1002'),
       ('2020-02-02','1003'),
       ('2020-02-02','1004'),
       ('2020-03-02','1001'),
       ('2020-03-02','1002'),
       ('2020-04-02','1005'),
       ('2020-05-02','1006');

2 数据分析

完整的代码如下:

select month,
      sum(month_cnt) over(order by month) as result
from(
    select month,
           lag(next_month_cnt,1,0) over(order by month) as month_cnt
    from(
        select distinct 
               t0.month as month,
               sum(if(!array_contains(t1.lead_stu_id_arr,t0.stu_id),1,0)) over(partition by t0.month) as next_month_cnt
        from
            (select 
                  date_format(dt,'yyyy-MM') as month,
                  stu_id
            from test19) t0
        left join
        (
            select month,
                   lead(stu_id_arr,1) over(order by month) as lead_stu_id_arr
            from(
                 select date_format(dt,'yyyy-MM') as month,
                        collect_list(stu_id) as stu_id_arr
                 from test19
                 group by date_format(dt,'yyyy-MM') 
                ) tmp1
        ) t1
        on t0.month = t1.month
    ) tmp2
) tmp3;

第一步:聚合每个月的stu_id,利用collect_list()函数(不去重)合并,具体sql如下:

select date_format(dt,'yyyy-MM') as month,
       collect_list(stu_id) as stu_id_arr
from test19
group by date_format(dt,'yyyy-MM') 

计算结果如下:

2020-01	[1001,1002]
2020-02	[1001,1002,1003,1004]
2020-03	[1001,1002]
2020-04	[1005]
2020-05	[1006]

第二步:按照月份排序,获取下一月合并之后的值,sql如下:

 select month,
        stu_id_arr,
        lead(stu_id_arr,1) over(order by month) as lead_stu_id_arr
from(
       select
                date_format(dt,'yyyy-MM') as month,
                collect_list(stu_id) as stu_id_arr
       from test19
       group by date_format(dt,'yyyy-MM')
     ) tmp1;

计算结果如下:

2020-01	[1001,1002]	[1001,1002,1003,1004]
2020-02	[1001,1002,1003,1004]	[1001,1002]
2020-03	[1001,1002]	[1005]
2020-04	[1005]	[1006]
2020-05	[1006]	NULL

     ps:总体思路是利用数组差集函数求出差值集合后,再利用size()求出具体的个数,最后sum聚合即可。hive中的数组函数array_contains可以实现这个需求,该函数表示在数组中查询某个元素是否存在。在该题目中,借助此函数判断 当月某个学生id是否在下月(数据集合 -->数组)中存在,如果存在就为0,不存在标记为1。

 第三步:利用步骤2的结果与原表进行关联,获取当前学生id

select
    t0.*,
    t1.*
from (select
          date_format(dt, 'yyyy-MM') as month,
          stu_id
      from test19) t0
 left join ( select
                   month,
                   lead(stu_id_arr, 1) over (order by month) as lead_stu_id_arr
             from ( select
                          date_format(dt, 'yyyy-MM') as month,
                          collect_list(stu_id)       as stu_id_arr
                  from test19
                  group by date_format(dt, 'yyyy-MM')
                 ) tmp1
             ) t1
on t0.month = t1.month;

结果如下:

2020-01	1001	2020-01	[1001,1002,1003,1004]
2020-01	1002	2020-01	[1001,1002,1003,1004]
2020-02	1001	2020-02	[1001,1002]
2020-02	1002	2020-02	[1001,1002]
2020-02	1003	2020-02	[1001,1002]
2020-02	1004	2020-02	[1001,1002]
2020-03	1001	2020-03	[1005]
2020-03	1002	2020-03	[1005]
2020-04	1005	2020-04	[1006]
2020-05	1006	2020-05	NULL

第四步:利用array_contains()函数判断当月的stu_id是否在下个月array数组中,如果存在标记0,不存在标记1。具体sql如下:

        select t0.month,
               t0.stu_id,
              if(!array_contains(t1.lead_stu_id_arr,t0.stu_id),1,0) as flag
        from
            (select
                   date_format(dt,'yyyy-MM') as month,
                   stu_id
            from test19) t0
        left join
        (
            select month,
                   lead(stu_id_arr,1) over(order by month) as lead_stu_id_arr
            from(
                 select date_format(dt,'yyyy-MM') as month,
                        collect_list(stu_id) as stu_id_arr
                 from test19
                 group by date_format(dt,'yyyy-MM')
                ) tmp1
        ) t1
        on t0.month = t1.month

结果如下:

2020-01	1001	0
2020-01	1002	0
2020-02	1001	0
2020-02	1002	0
2020-02	1003	1
2020-02	1004	1
2020-03	1001	1
2020-03	1002	1
2020-04	1005	1
2020-05	1006	1

第五步:基于步骤四的结果,按照月份分组,对flag求和,得到下个月的学生退费人数

select  distinct t0.month,
       -- 求解下个月的退费人数
        sum(if(!array_contains(t1.lead_stu_id_arr,t0.stu_id),1,0)) over(partition by t0.month) as next_month_cnt
from  (select
             date_format(dt,'yyyy-MM') as month,
             stu_id
       from test19) t0
left join
        ( select month,
                 lead(stu_id_arr,1) over(order by month) as lead_stu_id_arr
          from( select 
                        date_format(dt,'yyyy-MM') as month,
                        collect_list(stu_id) as stu_id_arr
                 from test19
                 group by date_format(dt,'yyyy-MM')
                ) tmp1
        ) t1
on t0.month = t1.month;

计算结果如下:

注意:第二列求是下个月的退费人数。

2020-01	0
2020-02	2
2020-03	2
2020-04	1

第六步:计算当前月的退费人数

    步骤五计算的是下一个月的学生退费人数,再利用 lag(next_month_cnt,1,0) over(order by month) 向上偏移一行,就得到当前月的退费人数

sql代码如下:

select month, 
      --基于下月的退费人数month_cnt字段,向上偏移一行,就得到当前月的退费人数
       lag(next_month_cnt,1,0) over(order by month) as month_cnt
 from(
        select distinct t0.month as month,
               sum(if(!array_contains(t1.lead_stu_id_arr,t0.stu_id),1,0)) over(partition by t0.month) as next_month_cnt
        from
            (select
                  date_format(dt,'yyyy-MM') as month,
                  stu_id
            from test19) t0
        left join
        (
            select month,
                   lead(stu_id_arr,1) over(order by month) as lead_stu_id_arr
            from(
                 select date_format(dt,'yyyy-MM') as month,
                        collect_list(stu_id) as stu_id_arr
                 from test19
                 group by date_format(dt,'yyyy-MM')
                ) tmp1
        ) t1
        on t0.month = t1.month
    ) tmp2;

计算结果如下:

2020-01	0
2020-02	0
2020-03	2
2020-04	2
2020-05	1

计算截止到当前月的退费人数,sql代码如下:

select month,
       -- sum() over(order by ..) 窗口计算范围:上无边界(起始行)到当前行
       sum(month_cnt) over(order by month) as result
from(
    select month,
          lag(next_month_cnt,1,0) over(order by month) as month_cnt
    from(
        select distinct t0.month as month,
               sum(if(!array_contains(t1.lead_stu_id_arr,t0.stu_id),1,0)) over(partition by t0.month) as next_month_cnt
        from
            (select
                  date_format(dt,'yyyy-MM') as month,
                  stu_id
            from test19) t0
        left join
        (
            select month,
                   lead(stu_id_arr,1) over(order by month) as lead_stu_id_arr
            from(
                 select date_format(dt,'yyyy-MM') as month,
                        collect_list(stu_id) as stu_id_arr
                 from test19
                 group by date_format(dt,'yyyy-MM')
                ) tmp1
        ) t1
        on t0.month = t1.month
    ) tmp2
) tmp3;

计算结果为:

2020-01	0
2020-02	0
2020-03	2
2020-04	4
2020-05	5

3 小结

   针对存在性问题,一般的求解思路是:1.利用collect_set()或者 collect_list()函数进行聚合,将数据集转换成数据组。2.再利用array_contains()等函数判断集合(数组)中是否存在某元素,针对结果打上标签。3.再根据标签进行之后的分组聚合计算等。

ps:以上文章参考:

https://blog.csdn.net/godlovedaniel/article/details/119388498?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167921970316800184142859%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167921970316800184142859&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-119388498-null-null.142^v74^control_1,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=%E5%AD%98%E5%9C%A8%E6%80%A7%E9%97%AE%E9%A2%98&spm=1018.2226.3001.4187文章浏览阅读741次。本文对存在性问题进行了探讨和研究,此类问题往往需要对不同的记录做对比分析,我们可以先将符合条件的数据域按照collect_set()或collect_list()函数进行聚合转换成数组,然后获取历史的数据域放入当前行,最后利用hive中数组的相关处理手段进行对比分析。常用的hive数组处理函数如expode()、size()、array()、array_contains()等函数,本题就借助于hive ,array_contains()函数进行存在性问题分析。_sql 求截止当前月退费总人数https://blog.csdn.net/godlovedaniel/article/details/119388498?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167921970316800184142859%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167921970316800184142859&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-119388498-null-null.142%5Ev74%5Econtrol_1,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&utm_term=%E5%AD%98%E5%9C%A8%E6%80%A7%E9%97%AE%E9%A2%98&spm=1018.2226.3001.4187

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1426840.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

物联网可视化平台:赋能企业数字化转型

在数字化转型的大潮中&#xff0c;企业面临着如何更好地理解和利用海量数据的挑战。物联网技术的快速发展&#xff0c;为企业提供了一个全新的视角和解决方案。通过物联网可视化平台&#xff0c;企业能够实时监控、分析和展示物联网数据&#xff0c;从而加速数字化转型的进程。…

深度揭秘:代理IP的工作原理及其在网络安全中的关键角色

代理IP的工作原理及其在网络安全中的关键角色是一个相对复杂但非常重要的主题。以下是对这一内容的深度揭秘&#xff1a; 代理IP的工作原理 1. 请求转发 当一个客户端&#xff08;如浏览器或爬虫程序&#xff09;使用代理IP时&#xff0c;它不是直接与目标网站通信&#xff0c…

【无刷电机学习】电流采样电路硬件方案

【仅作自学记录&#xff0c;不出于任何商业目的】 目录 AD8210 INA282 INA240 INA199 AD8210 【AD8210数据手册】 在典型应用中&#xff0c;AD8210放大由负载电流通过分流电阻产生的小差分输入电压。AD8210抑制高共模电压(高达65V)&#xff0c;并提供接地参考缓冲输出&…

从0搭建react+ts+redux+axios+antd项目

文章目录 一、安装及初始化二、TypeScript配置三、Webpack配置四、Prettier统一编码风格五、使用less六、Antd 安装及使用七、添加Router及配置八、安装axios九、添加redux及使用 本文介绍了如何用creat-react-app脚手架搭建一个react项目的基本结构&#xff0c;同时配置webpac…

书客、米家、柏曼大路灯哪款好?多维度实测对比推荐!

每到寒暑假&#xff0c;各个论坛上出现“大路灯怎么选”的类似话题非常频繁&#xff0c;因为现在的孩子出来上学期间需要读写之外&#xff0c;在寒暑假时也在不断的学习&#xff0c;许多家长关注到孩子学习时的光线问题&#xff0c;担心影响到孩子的视力状况&#xff0c;都纷纷…

Java的Mysql使用

Java的Mysql使用 说明 通过Java的方式连接Mysql中的数据库&#xff0c;并对数据库中的数据进行增加 查询操作 ​ 使用Mysql所提供的第三方库中的类(Mysql的API) 对其进行操作 ​ 将Mysql Jar包添加到lib目录后&#xff0c;就可以使用其中的类对其Mysql数据库进行操作 Mysq…

代码随想录算法训练营第五十九天|503.下一个更大元素II 、42. 接雨水

代码随想录算法训练营第五十九天|503.下一个更大元素II 、42. 接雨水 下一个更大元素II 503.下一个更大元素II 文章讲解&#xff1a;https://programmercarl.com/0503.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7%B4%A0II.html 题目链接&#xff1a;https://…

2024年美赛 (C题MCM)| 温网积分 |数学建模完整代码+建模过程全解全析

当大家面临着复杂的数学建模问题时&#xff0c;你是否曾经感到茫然无措&#xff1f;作为2022年美国大学生数学建模比赛的O奖得主&#xff0c;我为大家提供了一套优秀的解题思路&#xff0c;让你轻松应对各种难题。 让我们来看看美赛的C题&#xff01; 完整内容可以在文章末尾领…

2024美赛数学建模F题思路分析 - 减少非法野生动物贸易

1 赛题 问题F&#xff1a;减少非法野生动物贸易 非法的野生动物贸易会对我们的环境产生负面影响&#xff0c;并威胁到全球的生物多样性。据估计&#xff0c;它每年涉及高达265亿美元&#xff0c;被认为是全球第四大非法交易。[1]你将开发一个由数据驱动的5年项目&#xff0c;…

服务器C盘突然满了,是什么问题

随着时代的发展、互联网的普及&#xff0c;加上近几年云计算服务的诞生以及大规模普及&#xff0c;对于服务器的使用目前是非常普遍的&#xff0c;用户运维的主要对象一般也主要是服务器方面。在日常使用服务器的过程中&#xff0c;我们也会遇到各式各样的问题。最近就有遇到用…

SpringBoot RestTemplate 设置挡板

项目结构 代码 BaffleConfig /*** Description 记录配置信息* Author wjx* Date 2024/2/1 14:47**/ public interface BaffleConfig {// 是否开启挡板的开关public static boolean SWITCH true;// 文件根目录public static String ROOT_PATH "D:\\TIS\\mock";// …

Attack Lab:Phase1~Phase5【缓冲区溢出实验】

注&#xff1a;本实验所用文件不是csapp官网给出的&#xff0c;是学校下发的。可以参考我的思路。 phase 1 本阶段目标是使getbuf调用结束后&#xff0c;控制权交给touch1函数。 则我们要知道两件事&#xff1a;一是缓冲区大小&#xff0c;二是touch1在虚拟内存中的位置。 用…

如何取消隐藏Excel中的行?这里提供详细步骤

取消隐藏Microsoft Excel电子表格中的所有行就像按下键盘快捷键或使用功能区上的按钮一样简单。我们将向你展示如何操作。 如何使用快捷方式取消隐藏Excel中的所有行 若要在电子表格中显示隐藏行&#xff0c;请使用Microsoft Excel启动电子表格。然后&#xff0c;访问包含隐藏…

P1083 [NOIP2012 提高组] 借教室

P1083 [NOIP2012 提高组] 借教室 题目描述 在大学期间&#xff0c;经常需要租借教室。大到院系举办活动&#xff0c;小到学习小组自习讨论&#xff0c;都需要向学校申请借教室。教室的大小功能不同&#xff0c;借教室人的身份不同&#xff0c;借教室的手续也不一样。 面对海…

【项目日记(七)】第三层: 页缓存的具体实现(上)

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:项目日记-高并发内存池⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你做项目   &#x1f51d;&#x1f51d; 开发环境: Visual Studio 2022 项目日…

Armv8-M的TrustZone技术之在安全状态和非安全状态之间切换

Armv8-M安全扩展允许在安全和非安全软件之间直接调用。 Armv8-M处理器提供了几条指令来处理状态转换: 下图显示了安全状态转换。 如果入口点的第一条指令是SG且位于非安全可调用内存位置中,则允许从非安全到安全软件的直接API函数调用。 当非安全程序调用安全API时,API通过…

Linux系统管理和Shell脚本笔试题

1、写一个sed命令&#xff0c;修改/tmp/input.txt文件的内容&#xff0c;要求&#xff1a;(1) 删除所有空行&#xff1b;(2) 在非空行前面加一个"AAA"&#xff0c;在行尾加一个"BBB"&#xff0c;即将内容为11111的一行改为&#xff1a;AAA11111BBB #写入内…

JavaWeb前端——HTML/CSS

HTML/CSS概述 HTML&#xff1a;学习标签&#xff0c;CSS&#xff1a;学习样式 HTML 1. 不区分大小写。 2. 属性可以使用单引号/双引号 3. 在记事本/编辑器中编写html语言&#xff0c;通过浏览器解析渲染语言 4. 语法结构松散&#xff08;编写时要尽量严谨&#xff09; VSc…

github请求超时解决方法

github请求超时解决办法 我使用windows执行如下git命令,提示超时 git clone xxxxx命令行提示如下&#xff1a; Failed to connect to github.com port 443: Timed out问题排查 可我Chrome可以正常访问github甚至ChatGPT&#xff0c;但是为什么在命令行里面却无法访问&#…

AI大模型开发架构设计(7)——人人都需要掌握的AI编程及应用案例实战

文章目录 人人都需要掌握的AI编程及应用案例实战1 AI代码生成模型与AI编程助手介绍程序设计方式的发展自动代码生成AI编程工具 2 AI编程助手的代码生成模型架构剖析以 CodeGeeX 为例-发展过程以 CodeGeeX 为例-训练过程以 CodeGeeX 为例-大规模代码数据处理以 CodeGeeX 为例-模…