32-Hive SQL DML语法之查询数据

1.Select语法树

复制代码
从哪里查询取决于FROM关键字后面的table_reference,
这是我们写查询SQL的首先要确定的事即你查询谁?
表名和列名不区分大小写。

案例:在数据集中有一份数据文件《us-covid19-counties.dat》,里面记录了2021-01-28美国各个县累计新冠确诊病例数和累计死亡病例数。

bash 复制代码
use liushao;
drop table if exists t_usa_covid19;
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 data local inpath '/export/data/hivedata/covid19-counties.dat' into table liushao.t_usa_covid19;


select * from t_usa_covid19;

--  1. 查询 美国鬼子  美国的县郡  哪个洲 死了多少人
select county,state,deaths from t_usa_covid19;

select 1 from t_usa_covid19;

-- 2.我想看一下你目前正在使用哪个数据库
select  current_database();


--  3.匹配所有的行 (看 洲)
select state from t_usa_covid19;
-- 相当于
select all state from t_usa_covid19;


-- 4.数据去除重复
select distinct state from t_usa_covid19;

-- 5.使用条件
-- 一大于二十不成立的 返回 false ,不能查询到结果
select * from t_usa_covid19 where 1 > 2;

-- 六等于六成立 返回 true ,能查询到结果
select * from t_usa_covid19 where 6 =6;


-- 6.查找加利福尼亚州的 疫情数据
select * from t_usa_covid19 where state='California';

--  7.使用一下函数  长度  length() 查询州字母长度超过10位的
select * from t_usa_covid19 where length(state)>10;


--  8. 查询死亡人数超过100  的州  不能执行
--注意:where条件中不能使用聚合函数
-- --报错 SemanticException:Not yet supported place for UDAF 'sum'
--聚合函数要使用它的前提是结果集已经确定。
--而where子句还处于"确定"结果集的过程中,因而不能使用聚合函数。
-- select state,sum(deaths) from t_usa_covid19 where sum(deaths)>100 group by state;

select state,sum(deaths) from t_usa_covid19 group by state having sum(deaths)>100 ;

--9 查询 美国共有多少个的县郡   结果是: 3245

select  county from t_usa_covid19;

select  count(county) as county_nums from t_usa_covid19;

-- 去除重复的县郡  1930
select  count(distinct county) as county_nums from t_usa_covid19;



-- 10 查询一下美国加州有多少个县
select count(county) from t_usa_covid19 where state='California';

-- 11 统计德州总的死亡病例数
select sum(deaths) from  t_usa_covid19 where state='Texas';

-- 12 统计美国最高确诊病例是哪个县
select  max(cases) from  t_usa_covid19;


-- 回去 看看  group by   limit   order by  等 。。。
相关推荐
lusklusklusk几秒前
使用Visual Studio一步步创建并部署SSRS_PBIRS报表:数据源配置、图表设计、参数化查询、部署参数与排错实战
数据库·ide·visual studio
loopfde成序2 小时前
Playwright 与 CDP 多账号并发采集架构:标签页生命周期治理与联网问答证据链留存实践
数据库·架构
毅炼4 小时前
多 Agent 不会自动提速:先看任务图和改动怎么合
数据库·软件工程
GeW4 小时前
入行 Linux 与数据库运维:一条路径、三项习惯、一个考证观
linux·数据库
拖延症晚期的Liu6 小时前
数据表的操作
大数据·数据库·sql
FungLeo9 小时前
FengCMS · PHP+MySQL 升级实录 · 数据库驱动封装:mysqli 预处理与 affected_rows 关闭后变 -1 的坑
数据库·mysql·php·工程实践·fengcms
YYYing.10 小时前
【Redis剑指offer系列 (二) 】Redis线程模型
数据库·redis·缓存·八股
维爱智VR认知学能10 小时前
Python SQLite本地训练记录表迁移失败回滚与事务验证
数据库·python·单元测试·sqlite·软件工程
OpenCSG11 小时前
客户案例|Dell携手OpenCSG打造AI原生超节点
数据库·人工智能·ai-native
TDengine (老段)11 小时前
TDengine IDMP ---- Excel 插件
大数据·数据库·excel·时序数据库·tdengine·涛思数据