1.聚合函數(shù)
- 常見的聚合函數(shù):Count、Sum、Max、Min和Avg
- 特點:不管原始數(shù)據(jù)多少條,聚合之后只有一條
- Count(column)返回某列的行數(shù),不包括NULL值
2.GROUP BY
- select中的字段要么是GROUP BY字段,要么是被聚合函數(shù)應用的字段
2.HAVING
- WHERE中無法出現(xiàn)聚合函數(shù),所以有了HAVING
- WHERE是分組前過濾,HAVING是分組后過濾
為什么WHERE中不能使用聚合函數(shù)?
- 因為使用WHERE的時候,只能從表格字段中直接查找然后過濾,如果用到計算函數(shù),不是表格現(xiàn)有的直接可以查到的,就不可以作為過濾條件,對于表格現(xiàn)有的 length() 等函數(shù)還是可以的
- 而且因為WHERE的執(zhí)行順序在GROUP BY之前,而聚合函數(shù)只有在GROUP BY之后才能進行,因此WHERE中不能有聚合函數(shù)
使用having時一定要注意和select中的某個聚合函數(shù)一樣,且可以使用其別名。雖然having在select之前,但是就是可以,這樣SQL的性能更好,不用執(zhí)行兩遍聚合函數(shù)了
3.ORDER BY
- 默認按照升序(ASC),降序使用DESC
select * from t order by name desc, age asc; --先根據(jù)name降序,再根據(jù)age升序
4.LIMIT
- 用于限制SELECT語句返回的行數(shù)
- 接收一個或者兩個參數(shù),都是非負整數(shù)
- 第一個參數(shù)指定要返回的第一行的偏移量,第二個參數(shù)指定要返回的最大行數(shù)。單個參數(shù)時,偏移量默認為0。
5.執(zhí)行順序
6.Join
內(nèi)連接
左連接文章來源:http://www.zghlxwxcb.cn/news/detail-727253.html
7.函數(shù)分類(UDF,UDAF,UDTF)
show functions; //查看當下可用的所有函數(shù)
describe function extended funcname; //查看funcname的使用
文章來源地址http://www.zghlxwxcb.cn/news/detail-727253.html
8.常用內(nèi)置函數(shù)
------------String Functions 字符串函數(shù)------------
select length("itcast");
select reverse("itcast");
select concat("angela","baby");
--帶分隔符字符串連接函數(shù):concat_ws(separator, [string | array(string)]+)
select concat_ws('.', 'www', array('itcast', 'cn'));
--字符串截取函數(shù):substr(str, pos[, len]) 或者 substring(str, pos[, len])
select substr("angelababy",-2); --pos是從1開始的索引,如果為負數(shù)則倒著數(shù)
select substr("angelababy",2,2);
--分割字符串函數(shù): split(str, regex)
--split針對字符串數(shù)據(jù)進行切割 返回是數(shù)組array 可以通過數(shù)組的下標取內(nèi)部的元素 注意下標從0開始的
select split('apache hive', ' ');
select split('apache hive', ' ')[0];
select split('apache hive', ' ')[1];
----------- Date Functions 日期函數(shù) -----------------
--獲取當前日期: current_date
select current_date();
--獲取當前UNIX時間戳函數(shù): unix_timestamp
select unix_timestamp();
--日期轉(zhuǎn)UNIX時間戳函數(shù): unix_timestamp
select unix_timestamp("2011-12-07 13:01:03");
--指定格式日期轉(zhuǎn)UNIX時間戳函數(shù): unix_timestamp
select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
--UNIX時間戳轉(zhuǎn)日期函數(shù): from_unixtime
select from_unixtime(1618238391);
select from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');
--日期比較函數(shù): datediff 日期格式要求'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'
select datediff('2012-12-08','2012-05-09');
--日期增加函數(shù): date_add
select date_add('2012-02-28',10);
--日期減少函數(shù): date_sub
select date_sub('2012-01-1',10);
----Mathematical Functions 數(shù)學函數(shù)-------------
--取整函數(shù): round 返回double類型的整數(shù)值部分 (遵循四舍五入)
select round(3.1415926);
--指定精度取整函數(shù): round(double a, int d) 返回指定精度d的double類型
select round(3.1415926,4);
--取隨機數(shù)函數(shù): rand 每次執(zhí)行都不一樣 返回一個0到1范圍內(nèi)的隨機數(shù)
select rand();
--指定種子取隨機數(shù)函數(shù): rand(int seed) 得到一個穩(wěn)定的隨機數(shù)序列
select rand(3);
-----Conditional Functions 條件函數(shù)------------------
--使用之前課程創(chuàng)建好的student表數(shù)據(jù)
select * from student limit 3;
--if條件判斷: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
select if(1=2,100,200);
select if(sex ='男','M','W') from student limit 3;
--空值轉(zhuǎn)換函數(shù): nvl(T value, T default_value)
select nvl("allen","itcast");
select nvl(null,"itcast");
--條件轉(zhuǎn)換函數(shù): CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end;
select case sex when '男' then 'male' else 'female' end from student limit 3;
到了這里,關于Hive:聚合函數(shù)、GROUP BY、ORDER BY、LIMIT、執(zhí)行順序和JOIN、函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!