首页 > 其他分享 >Hive select查询语句

Hive select查询语句

时间:2023-07-31 10:35:14浏览次数:30  
标签:语句 usa -- covid19 state Hive where select

 

创建表

CREATE TABLE t_usa_covid19(
    count_date string,
    county string,
    state string,
    fips int,
    cases int,
    deaths int)
row format delimited fields terminated by ",";

--将数据load加载到t_usa_covid19表对应的路径下
load data local inpath '/root/hivedata/us-covid19-counties.dat' into table t_usa_covid19;
--查询所有字段或者指定字段
select *  from t_usa_covid19;
--查询某些字段
select county, cases, deaths from t_usa_covid19;
--查询常数返回 此时返回的结果和表中字段无关
select 1 from t_usa_covid19;
--查询当前数据库
select current_database(); --省去from关键字

去重查询走的是底层的MR,运行效率很低,所以,运行时间会很长,需要等待一下。

整体去重,两个字段都一样,才去重。

--2、ALL DISTINCT
--返回所有匹配的行
select state from t_usa_covid19;
--相当于
select all state from t_usa_covid19;

--返回所有匹配的行 去除重复的结果
select distinct state from t_usa_covid19;
--多个字段distinct 整体去重
select distinct county,state from t_usa_covid19;
--3、WHERE CAUSE
select * from t_usa_covid19 where 1 > 2;  -- 1 > 2 返回false
select * from t_usa_covid19 where 1 = 1;  -- 1 = 1 返回true

--找出来自于California州的疫情数据
select * from t_usa_covid19 where state = 'California';
--where条件中使用函数 找出州名字母长度超过10位的有哪些
select * from t_usa_covid19 where length(state) >10 ;
--注意:where条件中不能使用聚合函数
-- --报错 SemanticException:Not yet supported place for UDAF 'count'
--聚合函数要使用它的前提是结果集已经确定。
--而where子句还处于“确定”结果集的过程中,因而不能使用聚合函数。
select state,sum(deaths) from t_usa_covid19 where sum(deaths) >100 group by state;
--可以使用Having实现
select state,sum(deaths) from t_usa_covid19  group by state having sum(deaths) > 100;

第一个是错的 ,第二个才是对的

--4、聚合操作
--统计美国总共有多少个县county
select county as itcast from t_usa_covid19;
--学会使用as 给查询返回的结果起个别名
select count(county) as county_cnts from t_usa_covid19;
--去重distinct
select count(distinct county) as county_cnts from t_usa_covid19;

--统计美国加州有多少个县
select count(county) from t_usa_covid19 where state = "California";
--统计德州总死亡病例数
select sum(deaths) from t_usa_covid19 where state = "Texas";
--统计出美国最高确诊病例数是哪个县
select max(cases) from t_usa_covid19;
--5、GROUP BY

select *
from t_usa_covid19;

--根据state州进行分组 统计每个州有多少个县county
select count(county) from t_usa_covid19 where count_date = "2021-01-28" group by state;

--想看一下统计的结果是属于哪一个州的
select state,count(county) as county_nums from t_usa_covid19 where count_date = "2021-01-28" group by state;

--再想看一下每个县的死亡病例数,我们猜想很简单呀  把deaths字段加上返回  真实情况如何呢?
select state,count(county),sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" group by state;
--很尴尬 sql报错了org.apache.hadoop.hive.ql.parse.SemanticException:Line 1:27 Expression not in GROUP BY key 'deaths'

--为什么会报错??group by的语法限制
--结论:出现在GROUP BY中select_expr的字段:要么是GROUP BY分组的字段;要么是被聚合函数应用的字段。
--deaths不是分组字段 报错
--state是分组字段 可以直接出现在select_expr中

--被聚合函数应用
select state,count(county),sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" group by state;
--6、having
--统计2021-01-28死亡病例数大于10000的州
select state,sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" and sum(deaths) >10000 group by state;
--where语句中不能使用聚合函数 语法报错

--先where分组前过滤,再进行group by分组, 分组后每个分组结果集确定 再使用having过滤
select state,sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" group by state having sum(deaths) > 10000;
--这样写更好 即在group by的时候聚合函数已经作用得出结果 having直接引用结果过滤 不需要再单独计算一次了
select state,sum(deaths) as cnts from t_usa_covid19 where count_date = "2021-01-28" group by state having cnts> 10000;
--7、order by
--根据确诊病例数升序排序 查询返回结果
select * from t_usa_covid19 ;
select * from t_usa_covid19 order by cases;
--不写排序规则 默认就是asc升序
select * from t_usa_covid19 order by cases asc;
--8、limit
--没有限制返回2021.1.28 加州的所有记录
select * from t_usa_covid19 where count_date = "2021-01-28" and state ="California";

--返回结果集的前5条
select * from t_usa_covid19 where count_date = "2021-01-28" and state ="California" limit 5;

--返回结果集从第1行开始 共3行
select * from t_usa_covid19 where count_date = "2021-01-28" and state ="California" limit 2,3;
--注意 第一个参数偏移量是从0开始的
--执行顺序
select state,sum(deaths) as cnts from t_usa_covid19
where count_date = "2021-01-28"
group by state
having cnts> 10000
limit 2;
--1、inner join
select e.id,e.name,e_a.city,e_a.street
from employee e inner join employee_address e_a
on e.id =e_a.id;

--等价于 inner join=join
select e.id,e.name,e_a.city,e_a.street
from employee e join employee_address e_a
on e.id =e_a.id;


--等价于 隐式连接表示法
select e.id,e.name,e_a.city,e_a.street
from employee e , employee_address e_a
where e.id =e_a.id;



--2、left join
select e.id,e.name,e_conn.phno,e_conn.email
from employee e left join employee_connection e_conn
on e.id =e_conn.id;

--等价于 left outer join
select e.id,e.name,e_conn.phno,e_conn.email
from employee e left outer join  employee_connection e_conn
on e.id =e_conn.id;

 

标签:语句,usa,--,covid19,state,Hive,where,select
From: https://www.cnblogs.com/lin513/p/17592773.html

相关文章

  • 在Mapper.xml中写复杂的动态SQL语句
    在Mapper.xml中写复杂的动态SQL语句原文链接:https://blog.csdn.net/qq_42108331/article/details/131398433说明:在三层架构开发中,使用Mybatis框架操作数据库有两种方式,一种是在Mapper类里的方法上加注解(@Select、@Insert等),另一种是在Mapper.xml文件的标签内写SQL语句。第二......
  • sql语句
    SQL通用语法SQL语句可以单行或多行书写,以分号(“;”)结尾。SQL语句可以使用空格/缩进来增强语句的可读性。MySQL数据库的SQL语句不区分大小写,关键字建议使用大写。注释:单行注释:--注释内容或#注释内容(MySQL特有)多行注释:/*注释内容*/SQL分类分类全称说明......
  • 22-Hive函数应用
    1.多字节分隔符1.1问题与需求【默认规则】Hive默认序列化类是LazySimpleSerDe,其只支持使用单字节分隔符(char)来加载文本数据,例如逗号、制表符、空格等等,默认的分隔符为”\001”。根据不同文件的不同分隔符,我们可以通过在创建表时使用rowformatdelimited来指定文件中的分......
  • 21-Hive运算符&函数
    1.Hive内置运算符整体上,Hive支持的运算符可以分为三大类:关系运算、算术运算、逻辑运算。官方参考文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF也可以使用下述方式查看运算符的使用方式:--显示所有的函数和运算符showfunctions;--查看运算......
  • 20-Hive-DML&DQL
    1.LoadDATA回想一下,当在Hive中创建好表之后,默认就会在HDFS上创建一个与之对应的文件夹,默认路径是由参数hive.metastore.warehouse.dir控制,默认值是/user/hive/warehouse。要想让Hive的表和结构化的数据文件产生映射,就需要把文件移到到表对应的文件夹下面,当然,可以在建......
  • 19-Hive-DDL
    数据定义语言(DataDefinitionLanguage,DDL)是SQL语言集中对数据库内部的对象结构进行创建,删除,修改等的操作语言,这些数据库对象包括database(schema)、table、view、index等。核心语法由CREATE、ALTER与DROP三个所组成。DDL并不涉及表内部数据的操作。在某些上下文中,该术......
  • 18-Hive入门&安装
    1.Hive概述1.1什么是Hive?ApacheHive是一款建立在Hadoop之上的开源数据仓库工具,可以将存储在Hadoop文件中的结构化、半结构化数据文件映射为一张数据库表,基于表提供了一种类似SQL的查询模型,称为Hive查询语言(HQL),用于访问和分析存储在Hadoop文件中的大型数据集。H......
  • MySQL学习-DML(Data Manipulation Language)数据--select语句02
    表连接:分为内连接和外连接,常用内连接。当需要同时显示多个表中字段时,就可以用表连接。内连接:仅选出两张表中互相匹配的记录外连接:还会选出其他不匹配的记录外连接包含左连接和右连接左连接: ......
  • Java学习-3.流程控制语句
    一、输入和输出println是printline的缩写,表示输出并换行。因此,如果输出后不想换行,可以用print():System.out.print("C.");System.out.println();System.out.println("END");Java还提供了格式化输出的功能。为什么要格式化输出?因为计算机表示的数据不一定适合人来阅读:public......
  • Spartacus CMS Feature selector 的实现明细
    有下面这段代码:import{createFeatureSelector,MemoizedSelector}from'@ngrx/store';import{CmsState,CMS_FEATURE,StateWithCms}from'../cms-state';exportconstgetCmsState:MemoizedSelector<StateWithCms,CmsState>=createFea......