题目1:这道题是查询出至少连续出现3次的数字
create table sql1_5(
id int,
num int
)
row format delimited
fields terminated by ' ';
INSERT INTO sql1_5 (id, num) VALUES
(1, 1),
(2, 1),
(3, 1),
(4, 2),
(5, 1),
(6, 2),
(7, 2);
sql如下:
with t1 as (
select *,row_number() over (partition by num order by id) ,
id - row_number() over (partition by num order by id) jyl from sql1_5
)
select num from t1 group by jyl,num having count(*) >=3;
题目2:请写出hive查询语句,各类型专利top 10申请人,以及对应的专利申请数
1)表名:t_patent_detail (专利明细表)
2)表字段:专利号(patent_id)、专利名称(patent_name)、专利类型(patent_type)、申请时间
(aplly_date)、授权时间(authorize_date)、申请人(apply_users)
3)说明:同一个专利,可以有1到多个申请人,多人之间按分号隔开。本表记录数约1万条。例
如
4)请写出hive查询语句,各类型专利top 10申请人,以及对应的专利申请数
建表语句以及数据:
create table t_patent_detail(
patent_id string,
patent_name string,
patent_type string,
aplly_date string,
authorize_date string,
apply_users string
);
INSERT INTO t_patent_detail (patent_id, patent_name, patent_type, aplly_date, authorize_date, apply_users)
VALUES
('CN201821413799.7', '一种用于合金板棒材的往复式感应加热设备', '发明创造', '2018-08-30', '2019-08-09', '朱春野'),
('CN201911153500.8', '转化DNA回收率的检测方法及引物', '发明创造', '2019-11-22', '2020-01-10', '陶启长;韦东'),
('CN202011144174.7', '一种经修饰荧光探针及其应用', '发明创造', '2020-10-23', '2020-11-20', '陶启长;韦东;余明伟'),
('CN201920792416.X', '适用于中型桌面仪器的隔离装置', '实用新型', '2019-05-29', '2020-04-17', '许亦琳;余明伟;杨华'),
('CN201920973176.3', '一种高性价离心管冻存盒', '实用新型', '2019-06-26', '2020-04-17', '许亦琳;余明伟;邬剑星;王敏生'),
('CN202110256789.4', '一种新型环保材料的制备方法', '发明创造', '2021-03-09', '2021-12-15', '李明'),
('CN202220345678.X', '便捷式电子设备支架', '实用新型', '2022-02-18', '2022-08-22', '张华'),
('CN202211123456.7', '智能物流配送系统及方法', '发明创造', '2022-09-16', '2023-02-28', '王强;赵晓'),
('CN202320456789.2', '多功能办公文具收纳盒', '实用新型', '2023-03-20', '2023-10-18', '孙悦;刘敏'),
('CN202310567890.1', '高效能源转换装置', '发明创造', '2023-04-12', '2023-11-09', '吴涛;周琳'),
('CN202410123456.8', '人工智能辅助教学方法', '发明创造', '2024-02-05', '2024-07-12', '郑宇;林悦'),
('CN202420234567.9', '可折叠户外遮阳伞', '实用新型', '2024-01-18', '2024-06-20', '陈晨;杨阳'),
('CN202120678901.2', '新型保温杯结构', '实用新型', '2021-04-06', '2021-10-25', '刘辉;张峰'),
('CN202210789012.3', '大数据分析处理平台', '发明创造', '2022-07-05', '2022-12-30', '马丽;王鹏'),
('CN202320890123.4', '创意灯具设计', '实用新型', '2023-06-15', '2023-12-08', '赵丹;李华'),
('CN202110345678.5', '一种新型太阳能电池技术', '发明创造', '2021-03-30', '2021-11-18', '王力'),
('CN202220456789.X', '便携式空气净化器', '实用新型', '2022-03-15', '2022-09-25', '陈雪'),
('CN202211234567.8', '智能医疗诊断系统', '发明创造', '2022-10-12', '2023-03-20', '刘阳;张辉'),
('CN202320567890.3', '可调节电脑桌', '实用新型', '2023-04-05', '2023-11-12', '李丽;王浩'),
('CN202310678901.4', '高效农业灌溉方法', '发明创造', '2023-05-10', '2023-12-15', '赵刚;孙强'),
('CN202410234567.9', '虚拟现实交互技术', '发明创造', '2024-03-08', '2024-08-20', '周明;吴俊'),
('CN202420345678.0', '防水运动手表', '实用新型', '2024-02-12', '2024-07-25', '林晓;郑凯'),
('CN202120789012.5', '新型雨伞设计', '实用新型', '2021-04-28', '2021-11-05', '杨波;刘悦'),
('CN202210890123.6', '区块链数据安全技术', '发明创造', '2022-07-25', '2022-12-12', '马宁;赵亮'),
('CN202320901234.7', '创意家居装饰品', '实用新型', '2023-07-02', '2023-12-20', '孙琳;李阳'),
('CN202411167890.1', '新型合金材料制备工艺', '发明创造', '2024-10-05', '2025-01-10', '朱春野;陶启长'),
('CN202420278901.X', '便捷式电子秤设计', '实用新型', '2024-02-20', '2024-08-05', '许亦琳;余明伟'),
('CN202411289012.2', '智能数据分析方法', '发明创造', '2024-11-12', '2025-02-25', '韦东;余明伟'),
('CN202420390123.X', '创意手机支架', '实用新型', '2024-03-18', '2024-09-20', '杨华;王敏生'),
('CN202411390124.3', '高效能源转化技术', '发明创造', '2024-11-25', '2025-03-15', '邬剑星;陶启长'),
('CN202420490125.4', '可折叠收纳袋', '实用新型', '2024-04-12', '2024-10-25', '刘敏;许亦琳'),
('CN202411490126.5', '智能物流管理系统', '发明创造', '2024-12-02', '2025-04-10', '王强;韦东'),
('CN202420590127.6', '多功能钥匙扣设计', '实用新型', '2024-05-08', '2024-11-15', '赵晓;余明伟'),
('CN202411590128.7', '新型环保材料研发', '发明创造', '2024-12-10', '2025-04-25', '李明;陶启长'),
('CN202420690129.8', '便携式手电筒', '实用新型', '2024-06-05', '2024-12-05', '张华;许亦琳'),
('CN202411690130.1', '智能医疗辅助设备', '发明创造', '2024-12-18', '2025-05-10', '王强;韦东;余明伟'),
('CN202420790131.2', '创意书签设计', '实用新型', '2024-07-02', '2025-01-20', '孙悦;刘敏'),
('CN202411790132.3', '高效农业生产技术', '发明创造', '2024-12-25', '2025-05-25', '吴涛;周琳'),
('CN202420890133.4', '可折叠扇子设计', '实用新型', '2024-07-20', '2025-02-15', '郑宇;林悦'),
('CN202411890134.5', '新型能源存储装置', '发明创造', '2024-12-30', '2025-06-10', '陈晨;杨阳'),
('CN202420990135.6', '便携式雨伞', '实用新型', '2024-08-15', '2025-03-05', '刘辉;张峰'),
('CN202411990136.7', '智能交通信号系统', '发明创造', '2025-01-05', '2025-06-25', '马丽;王鹏'),
('CN202421090137.8', '多功能文具盒设计', '实用新型', '2024-09-02', '2025-04-05', '赵丹;李华'),
('CN202412090138.9', '新型大数据处理技术', '发明创造', '2025-01-15', '2025-07-10', '王力;陈雪'),
('CN202421190139.X', '便携式水杯设计', '实用新型', '2024-09-20', '2025-04-20', '刘阳;张辉'),
('CN202412190140.2', '智能物流配送优化技术', '发明创造', '2025-01-25', '2025-07-25', '李丽;王浩'),
('CN202421290141.3', '创意笔记本设计', '实用新型', '2024-10-08', '2025-05-10', '赵刚;孙强'),
('CN202412290142.4', '高效能源利用技术', '发明创造', '2025-02-05', '2025-08-10', '周明;吴俊'),
('CN202421390143.5', '便携式餐具设计', '实用新型', '2024-10-25', '2025-05-25', '林晓;郑凯'),
('CN202412390144.6', '新型人工智能算法', '发明创造', '2025-02-15', '2025-08-25', '杨波;刘悦'),
('CN202421490145.7', '创意手机挂件设计', '实用新型', '2024-11-08', '2025-06-15', '马宁;赵亮'),
('CN202412490146.8', '大数据安全防护技术', '发明创造', '2025-02-25', '2025-09-10', '孙琳;李阳'),
('CN202421590147.9', '便携式化妆镜设计', '实用新型', '2024-11-20', '2025-06-20', '张宇;王丽'),
('CN202412590148.X', '智能交通预测技术', '发明创造', '2025-03-05', '2025-09-25', '李明;王强'),
('CN202421690149.0', '多功能钱包设计', '实用新型', '2024-12-05', '2025-07-10', '陈刚;赵勇'),
('CN202412690150.3', '高效农业灌溉优化技术', '发明创造', '2025-03-15', '2025-10-10', '周伟;吴昊'),
('CN202421790151.4', '创意钥匙链设计', '实用新型', '2024-12-20', '2025-07-20', '林悦;郑佳'),
('CN202412790152.5', '新型环保能源技术', '发明创造', '2025-03-25', '2025-10-25', '杨辉;刘梅'),
('CN202421890153.6', '便携式小风扇设计', '实用新型', '2025-01-02', '2025-08-05', '马俊;王涛'),
('CN202412890154.7', '智能医疗诊断优化技术', '发明创造', '2025-04-05', '2025-11-10', '孙丽;李华'),
('CN202421990155.8', '创意笔袋设计', '实用新型', '2025-01-20', '2025-08-20', '张勇;王辉'),
('CN202412990156.9', '新型材料应用技术', '发明创造', '2025-04-15', '2025-11-25', '陈燕;刘芳'),
('CN202422090157.X', '便携式充电宝设计', '实用新型', '2025-02-10', '2025-09-15', '李明;刘刚'),
('CN202413090158.0', '智能安防预警技术', '发明创造', '2025-04-25', '2025-12-10', '王芳;陈强'),
('CN202422190159.1', '创意手机壳设计', '实用新型', '2025-03-05', '2025-10-05', '赵斌;许亦琳'),
('CN202413190160.3', '高效能源回收技术', '发明创造', '2025-05-05', '2026-01-10', '朱春野;陶启长'),
('CN202422290161.4', '便携式耳机设计', '实用新型', '2025-03-20', '2025-10-20', '韦东;余明伟'),
('CN202413290162.5', '智能数据分析优化技术', '发明创造', '2025-05-20', '2026-01-25', '杨华;王敏生'),
('CN202422390163.6', '创意书签夹设计', '实用新型', '2025-04-05', '2025-11-15', '邬剑星;陶启长'),
('CN202413390164.7', '新型环保工艺优化技术', '发明创造', '2025-05-30', '2026-02-10', '刘敏;许亦琳'),
('CN202422490165.8', '便携式小台灯设计', '实用新型', '2025-04-25', '2025-12-05', '王强;韦东'),
('CN202413490166.9', '智能物流管理优化技术', '发明创造', '2025-06-15', '2026-02-25', '赵晓;余明伟'),
('CN202422590167.X', '创意笔记本封面设计', '实用新型', '2025-05-15', '2025-12-20', '李明;陶启长'),
('CN202413590168.0', '新型能源存储优化技术', '发明创造', '2025-06-30', '2026-03-10', '张华;许亦琳'),
('CN202422690169.1', '便携式梳子设计', '实用新型', '2025-06-10', '2026-01-05', '王强;韦东;余明伟'),
('CN202413690170.4', '智能医疗辅助设备优化技术', '发明创造', '2025-07-15', '2026-03-25', '孙悦;刘敏'),
('CN202422790171.5', '创意手机支架改良设计', '实用新型', '2025-07-05', '2026-02-05', '吴涛;周琳'),
('CN202413790172.6', '高效农业生产优化技术', '发明创造', '2025-08-05', '2026-04-10', '郑宇;林悦'),
('CN202422890173.7', '可折叠购物袋设计', '实用新型', '2025-08-15', '2026-02-20', '陈晨;杨阳'),
('CN202413890174.7', '新型大数据处理优化技术', '发明创造', '2025-08-30', '2026-04-25', '刘辉;张峰'),
('CN202422990175.8', '便携式餐具套装设计', '实用新型', '2025-09-10', '2026-03-15', '马丽;王鹏'),
('CN202413990176.8', '智能交通信号系统优化技术', '发明创造', '2025-09-25', '2026-05-10', '赵丹;李华'),
('CN202423090177.9', '多功能文具盒改良设计', '实用新型', '2025-10-05', '2026-04-05', '王力;陈雪'),
('CN202414090178.9', '新型大数据分析技术', '发明创造', '2025-10-20', '2026-05-25', '刘阳;张辉'),
('CN202423190179.X', '便携式水杯改良设计', '实用新型', '2025-11-05', '2026-04-20', '李丽;王浩'),
('CN202414190180.2', '智能物流配送精准技术', '发明创造', '2025-11-20', '2026-06-10', '赵刚;孙强'),
('CN202423290181.3', '创意笔记本内页设计', '实用新型', '2025-12-05', '2026-05-15', '周明;吴俊'),
('CN202414290182.4', '高效能源利用优化技术', '发明创造', '2025-12-20', '2026-06-25', '林晓;郑凯'),
('CN202423390183.5', '便携式化妆镜改良设计', '实用新型', '2026-01-05', '2026-06-10', '杨波;刘悦'),
('CN202414390184.6', '新型人工智能应用技术', '发明创造', '2026-01-20', '2026-07-10', '马宁;赵亮'),
('CN202423490185.7', '创意手机挂件改良设计', '实用新型', '2026-02-05', '2026-07-20', '孙琳;李阳'),
('CN202414490186.8', '大数据安全防护优化技术', '发明创造', '2026-02-20', '2026-08-10', '张宇;王丽'),
('CN202423590187.9', '便携式小风扇改良设计', '实用新型', '2026-03-05', '2026-08-20', '李明;王强');
sql如下:
--各类型专利top 10申请人,以及对应的专利申请数
--先将申请人字段炸裂,注意这里的分隔符为中文的逗号
select t.*,users from t_patent_detail t lateral view explode(split(apply_users,";")) t1 as users;
with t as (
select t.*,users from t_patent_detail t lateral view explode(split(apply_users,";")) t1 as users
),t2 as (
select users `申请人`,count(1) `申请专利数`,rank() over (order by count(1) desc) `专利数排名` from t group by users
)select * from t2 where `专利数排名` <= 10;
题目3:现有用户登录记录表,请查询出用户连续三天登录的所有数据记录
数据如下:
1 2024-04-25
1 2024-04-26
1 2024-04-27
1 2024-04-28
1 2024-04-30
1 2024-05-01
1 2024-05-02
1 2024-05-04
1 2024-05-05
2 2024-04-25
2 2024-04-28
2 2024-05-02
2 2024-05-03
2 2024-05-04
建表语句:
create table user_log(
id int,
dt string
)
row format delimited
fields terminated by '\t';
load data local inpath '/home/sqltest/timu20.txt' into table user_log;
sql如下:
--第一步:求解每行日期后面第三行的日期 lead()和 真正第三天的日期
select*,
lead(dt,2) over(partition by id order by dt) later3dt,
date_add(dt,2) true3dt
from user_log;
--第二步:判断是否连续登录三天
with t as (
select*,
lead(dt,2) over(partition by id order by dt) later3dt,
date_add(dt,2) true3dt
from user_log
) select *,if(later3dt==true3dt,1,0) num from t;
--第三步:筛选出连续登录三天的每个起始日期
with t as (
select*,
lead(dt,2) over(partition by id order by dt) later3dt,
date_add(dt,2) true3dt
from user_log
) ,t1 as (
select *,if(later3dt==true3dt,1,0) num from t
)select * from t1 where num=1;
-- 第四步:表合并求最终结果(和一个三行的表进行合并)(笛卡尔积)
with t as (
select*,
lead(dt,2) over(partition by id order by dt) later3dt,
date_add(dt,2) true3dt
from user_log
) ,t1 as (
select *,if(later3dt==true3dt,1,0) num from t
),t2 as (
select * from t1 where num=1
) select id,dt,list,date_add(dt,d.list) dt2 from t2,(select explode(array(0,1,2)) list) d;
标签:02,专场,05,2024,2025,发明创造,sql,实用新型,10
From: https://blog.csdn.net/qq_62984376/article/details/143605702