服务器网站网站专用深圳网站建设公司企业

web/2025/10/1 20:53:55/文章来源:
服务器网站网站专用,深圳网站建设公司企业,私密性最好的浏览器,wordpress diy主题目录 一、原创文章被引用次数 0 问题描述 1 数据准备 2 数据分析 ​编辑 3 小结 二、学生退费人数 0 问题描述 1 数据准备 2 数据分析 3 小结 一、原创文章被引用次数 0 问题描述 求原创文章被引用的次数#xff0c;注意本题不能用关联的形式求解。 1 数据准备 i…目录 一、原创文章被引用次数 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 selectt1.id,count(t2.oid) as cnt from (select * from table18 where oid 0) t1left join(select * from table18 where oid 0) t2on t1.id t2.oid group by t1.id order by t1.id; 输出结果为 题意要求不能使用join等关联形式求解其实该题本质是存在性计数问题。 思路二借助array_contains(array,element) 函数 selectnew_id,sum(flag)as cnt from (selectid,oid,contains,-- 第二步利用array_contains()函数判断引用的oid是否在原创文章id集合中ture则记为1false则记为0if(array_contains(contains, oid), 1, 0) flag,-- 第三步清洗数据补充完整的原创文章if(array_contains(contains, oid), oid, if(oid 0, id, null)) new_idfrom ( -- 第一步构建原创文章id集合作为辅助列selectid,oid,collect_set(if(oid 0, id, null)) over () containsfrom 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作为辅助列。 selectid,oid,collect_set(if(oid 0, id, null)) over () contains from table18; 第二步利用array_contains()函数判断非原创的oid是否在原创文章id集合中存在则计数为1否则计数为0。 selectid,oid,contains,if(array_contains(contains, oid), 1, 0) as flag from ( selectid,oid,collect_set(if(oid 0, id, null)) over () containsfrom table18) tmp1; 第三步清洗数据对原创文章id补充完整 selectid,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 ( selectid,oid,collect_set(if(oid 0, id, null)) over () containsfrom 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 selectnew_id,sum(flag)as cnt from (selectid,oid,contains,-- 第二步利用array_contains()函数判断引用的oid是否在原创文章id集合中ture则记为1false则记为0if(array_contains(contains, oid), 1, 0) flag,-- 第三步清洗数据补充完整的原创文章idif(array_contains(contains, oid), oid, if(oid 0, id, null)) new_idfrom ( -- 第一步构建原创文章id集合作为辅助列selectid,oid,collect_set(if(oid 0, id, null)) over () containsfrom 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_cntfrom(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_cntfrom(select date_format(dt,yyyy-MM) as month,stu_idfrom test19) t0left join(select month,lead(stu_id_arr,1) over(order by month) as lead_stu_id_arrfrom(select date_format(dt,yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group by date_format(dt,yyyy-MM) ) tmp1) t1on 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(selectdate_format(dt,yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group 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] NULLps:总体思路是利用数组差集函数求出差值集合后再利用size()求出具体的个数最后sum聚合即可。hive中的数组函数array_contains可以实现这个需求该函数表示在数组中查询某个元素是否存在。在该题目中借助此函数判断 当月某个学生id是否在下月数据集合 --数组中存在如果存在就为0不存在标记为1。 第三步利用步骤2的结果与原表进行关联获取当前学生id selectt0.*,t1.* from (selectdate_format(dt, yyyy-MM) as month,stu_idfrom test19) t0left join ( selectmonth,lead(stu_id_arr, 1) over (order by month) as lead_stu_id_arrfrom ( selectdate_format(dt, yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group 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 flagfrom(selectdate_format(dt,yyyy-MM) as month,stu_idfrom test19) t0left join(select month,lead(stu_id_arr,1) over(order by month) as lead_stu_id_arrfrom(select date_format(dt,yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group by date_format(dt,yyyy-MM)) tmp1) t1on 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 (selectdate_format(dt,yyyy-MM) as month,stu_idfrom test19) t0 left join( select month,lead(stu_id_arr,1) over(order by month) as lead_stu_id_arrfrom( select date_format(dt,yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group 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_cntfrom(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_cntfrom(selectdate_format(dt,yyyy-MM) as month,stu_idfrom test19) t0left join(select month,lead(stu_id_arr,1) over(order by month) as lead_stu_id_arrfrom(select date_format(dt,yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group by date_format(dt,yyyy-MM)) tmp1) t1on 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_cntfrom(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_cntfrom(selectdate_format(dt,yyyy-MM) as month,stu_idfrom test19) t0left join(select month,lead(stu_id_arr,1) over(order by month) as lead_stu_id_arrfrom(select date_format(dt,yyyy-MM) as month,collect_list(stu_id) as stu_id_arrfrom test19group by date_format(dt,yyyy-MM)) tmp1) t1on t0.month t1.month) tmp2 ) tmp3; 计算结果为: 2020-01 0 2020-02 0 2020-03 2 2020-04 4 2020-05 53 小结 针对存在性问题一般的求解思路是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%257Drequest_id167921970316800184142859biz_id0utm_mediumdistribute.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_chatgptutm_term%E5%AD%98%E5%9C%A8%E6%80%A7%E9%97%AE%E9%A2%98spm1018.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%257Drequest_id167921970316800184142859biz_id0utm_mediumdistribute.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_chatgptutm_term%E5%AD%98%E5%9C%A8%E6%80%A7%E9%97%AE%E9%A2%98spm1018.2226.3001.4187

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

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

相关文章

公司网站上线流程江苏省建设工程协会网站

前言 本文主要讲述不同SQL语句的优化策略。 SQL | DML语句 insert语句 插入数据的时候,改为批量插入 插入数据的时候,按照主键顺序插入 大批量插入数据的时候(百万),用load指令,从本地文件载入&#x…

免费成品网站那里好浙江省永康市建设局网站进不去

git开发工作流程 (1)先将远程代码pull到本地 (2)在本地上分支上进行开发 (3)开发完之后,push到远程分支 (4)由远程的master进行所有分支合并

jsp网站制作详细教程大庆公司做网站

一、简介 PM2 是一个守护进程管理器,它将帮助您管理和保持您的应用程序在线。PM2 入门很简单,它以简单直观的 CLI 形式提供,可通过 NPM 安装。官网地址:https://pm2.keymetrics.io/ 二、问题:pm2日志内存占用过高&am…

北京做冷冻牛羊肉的网站怎样制作单页网站

Extend-application 方法扩展 eggjs的方法的扩展和编写 Egg.js可以对内部的五种对象进行扩展,以下是可扩展的对象、说明、this指向和使用方式。 application对象方法拓展 按照Egg的约定,扩展的文件夹和文件的名字必须是固定的。比如要对application扩…

做暧昧在线网站c2c模式的网站

案例用到的测试数据请参考文章: Flink自定义Source模拟数据流 原文链接:https://blog.csdn.net/m0_52606060/article/details/135436048 窗口的概念 Flink是一种流式计算引擎,主要是来处理无界数据流的,数据源源不断、无穷无尽。…

郑州网站高端网站设计网站备案号 怎么写

文章目录 问题描述原因分析问题分析问题解决问题描述 在使用pix4d处理航测数据时,部分航带高精度检测时,提示未校准相机: 287 out of 402 images calibrated (71%), all images enabled, 6 blocks 质量报告如下所示: 切换到【控三射线】,查看空山射线,红色部分是校准失败…

青岛网站seo优化重庆綦江网站制作公司推荐

题目 MASS 库中包含 Boston (波士顿房价)数据集,它记录了波士顿周围 506 个街区的 medv (房价中位数)。我们将设法用 13 个预测变量如 rm (每栋住宅的平均房间数), age (平均房 龄), lstat (社会经济地位低的家庭所占比例)等来预测…

模板网站最大缺点采购网官网

containerd镜像导出并导入docker 1 查看containerd 本地镜像列表 crictl images 2 containerd 导出本地镜像到当前目录下(注意: 导出导入需要指定镜像平台类型 --platform) ctr -n k8s.io images export nacos-server-24-06-30-13-02-…

沧州网站建设报价网站建设流程 知乎

文章目录 1 前缀和数组1.1 题解1.2 Code1.3 结果 2 二维矩阵的前缀和数组2.1 题解2.2 Code2.3 结果 3 差分数组 1 前缀和数组 适用于快速频繁的计算一个索引区间内的元素之和,核心思想就是使用一个前缀和数组,然后使用前缀和数组的两个元素之差&#xf…

一站式服务就像一个什么杭州建设网站职称人才工作专题

按照以下方法完美解决; https://blog.csdn.net/u014044812/article/details/78727496转载于:https://www.cnblogs.com/MichaelMeng/p/10415565.html

seo网站优化怎么做微信公众号如何做网站

写这篇文章之前,我去百度了一下啥叫锁,百度百科上写道:置于可启闭的器物上,以钥匙或暗码开启。确实我们一般理解的锁就是门锁,密码锁,但是在计算机科学中,锁又是啥,说实话&#xff0…

网站建设方网站登录流程

package 专题练习;import java.util.Scanner;public class marking_by_judges {//需求:在唱歌比赛中,有6名评委给选手打分,范围是[0,100]的整数.//选手最后得分为去掉最高分和最低分的平均分public static void main(String[] args) {//储存分数数组int[] score_six new int[6…

惠州做棋牌网站建设哪家公司收费合理树莓派做影视网站

一.表单伪造 之前一直用的 GET 请求方式,而表单可以实现 POST 方式,我们来实验下: 先在 TaskController 创建两个方法,一个表单页,一个接受表单数据路由; public function form() { return v…

做网站乱码灵宝seo公司

<自动自发> 第1章 序言 你属于哪种人&#xff1f; 我们经常会听到这些熟悉的话语&#xff1a; “现在是午餐时间&#xff0c;请您3点以后再打过来吧。” “那根本就不是我负责的工作。” “我现在很忙。” “那是汉曼的工作。” “我不知道该怎样帮助你。” “你去图书馆…

.net网站建设实例设置网站404

网站&#xff1a;http://www.sxdyc.com/visualsBoxHalfPlot 一、配对型的复杂箱线图简介 配对型的复杂箱线图原理与箱线图相同&#xff0c;常见于配对样本的数据分析中&#xff0c;在日常研究中&#xff0c;我们会碰到配对资料&#xff0c;例如同一病人治疗前后的变化&#xff…

网站建设错误代码50019室内装修网站

文章目录 数学函数函数使用 其它函数函数使用 数学函数 函数使用 其它函数 函数使用 user() 查询当前用户 database()显示当前正在使用的数据库 password()函数&#xff0c;MySQL数据库使用该函数对用户加密 md5(str)对一个字符串进行md5摘要&#xff0c;摘要后得到一个32…

商务网站建设工程师网站联盟广告

安装步骤 1、更新系统 apt update && apt upgrade && apt dist-upgrade 2、安装openvas apt-get install openvas 3、初始化 gvm-setup 时间要很久 4、检查安装结果 gvm-check-setup 安装成功 5、设置用户名和密码&#xff0c;都为admin sudo runuse…

建品牌网站公司中国空间站扩展

转载自 一次频繁Full GC的排查过程&#xff0c;根源居然是它... 业务部门的一个同事遇到个奇怪的 Full GC 问题&#xff0c;有个服务迁移到新的应用后&#xff0c;一直频繁 Full GC。新应用机器的配置是 4c 8g&#xff0c;老应用是 4c 4g&#xff0c;老应用 GC 都很正常&…

完备的网站建设wordpress排除分类目录文章

文章目录 1. SpinBox简介2. SpinBox使用2.1 可视化UI设计2.2 widget.h2.3 widget.cpp 1. SpinBox简介 QSpinBox 用于整数的显示和输入&#xff0c;一般显示十进制数&#xff0c;也可以显示二进制、十六进制的数&#xff0c;而且可以在显示框中增加前缀或后缀。 QDoubleSpinBox…