实例:创建临时表,一步完成计数

简介: 创建临时表,一步完成计数

实例:创建临时表,一步完成计数

select year,count(1) from

(select regexp_extract(title, "(\\d{4})",1)as year from yiqianbin_movie where title regexp '\\d{4}') temp

group by year;

image.png

补充

image.png

image.png

杂七杂八

select * from yiqianbin_movie where title not regexp '\\d{4}';

create table movie_year as select *,substr(title,-5,4) as year from yiqianbin_movie;

create table movies_year as select *,regexp_extract(title, "(\\d{4})",1)as year from yiqianbin_movie where title regexp '\\d{4}';

select substr(title,-5,4)from yiqianbin_movie where title regexp '\\d{4}';

select year,count(1) from movies_year group by year;

查出括号中的四个数字

select regexp_extract(title,"\\((\\d{4})\\)") from yiqianbin_movie;


查出哪一年出的电影最多,即年份出现最多的数字对应的电影名

select year,count(1) as year1 from movies_year group by year order by year1 desc limit 1;

将多个电影类型分开

先分开

select explode(split(genres,'\\|')) as type from yiqianbin_movie;  

之后将分开的导入一个新表

create table movies_type as select explode(split(genres,'\\|')) as type from yiqianbin_movie;

将类型的多少按降序排下来

select type,count(1) as count from movies_type group by type order by count desc;

临时表(一步完成)

select type,count(1) as count from (select explode(split(genres,'\\|')) as type from yiqianbin_movie)temp group by type order by count desc;

查出评分次数最高的电影

select movieId,count(1) as count from yiqianbin_ratings group by movieId order by count desc limit 1;

select * from(select movieId,count(1) as count from yiqianbin_ratings group by movieId order by count desc limit 1)t left join yiqianbin_movie m on t.movieId = m.movieId;


什么类型的电影评分最高      第四行是一个视图查询

1.先连接查询,每部电影的评分+分类

2.分类  行专列

3.按类型分组统计                                  

select type,avg(rating) as avgrating from

(select t1.rating,type.col as type from(select r.rating,m.genres from yiqianbin_ratings r

left join yiqianbin_movie m on r.movieId = m.movieId)t1

lateral view explode(split(t1.genres,'\\|')) type)t2

group by type order by avgrating desc

limit 10;


按年份统计电影数量

create table movies_year_result(year string,num string);

insert into movies_year_result

select year,count(1) from

(select substr(title,-5,4) as year from yiqianbin_movie where title regexp '\\d{4}')t group by year;

什么类型电影最多

create table genres_top10_result(type string,num string);

insert into genres_top10_result

select type,count(1) as total from

(select explode(split(genres,'\\|')) as type from yiqianbin_movie)t group by type

order by total desc limit 10;

评分人气最高电影top10

create table hot_top10_result(title string,rating string);

insert into hot_top10_result

select m.title,t.total from (

select movieId,count(1) as total

from yiqianbin_ratings group by movieId

order by total desc limit 10)t

left join yiqianbin_movie m on t.movieId = m.movieId;

高频词top10

create table tag_top10_result(tag string,num string);

insert into tag_top10_result

select tag,count(1) as total from

(select explode(split(tag,'\\s')) as tag from yiqianbin_tags)t

group by tag

order by total desc limit 10;

评分最高电影top10

create table rating_top10_result(title string,rating string);

insert into rating_top10_result

select m.title,t.avgrating from(

select movieId,avg(rating) as avgrating

from yiqianbin_ratings

group by movieId

order by avgrating desc limit 10)t

left join yiqianbin_movie m on t.movieId = m.movieId;

目录
相关文章
表格存储表删除后重建,为什么会占用空间
表格存储表删除后重建,为什么会占用空间
103 3
|
数据库
【教程】truncate清空表数据,为什么数据库的空间还是和原来一样并没有释放|数据库释放表空间教程
【教程】truncate清空表数据,为什么数据库的空间还是和原来一样并没有释放|数据库释放表空间教程
|
6月前
|
SQL 关系型数据库 MySQL
PolarDB产品使用问题之想要所有表执行是否需要时间分段执行
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
SQL 关系型数据库 MySQL
MySQL 查询重复数据,删除重复数据保留id最小的一条作为唯一数据
MySQL 查询重复数据,删除重复数据保留id最小的一条作为唯一数据
321 0
MySQL 查询重复数据,删除重复数据保留id最小的一条作为唯一数据
|
SQL
【如何成为SQL高手】第二关:表记录的插入、更新、删除
【如何成为SQL高手】第二关:表记录的插入、更新、删除
245 0
【如何成为SQL高手】第二关:表记录的插入、更新、删除
|
关系型数据库 MySQL
mysql某个表中数据量大删数据后空间不释放
mysql某个表中数据量大删数据后空间不释放

热门文章

最新文章

下一篇
开通oss服务