hive
课下练习
select count(*) from score;
select count(1) from score;
select count (score.score) from score;
explain select count (score.score) from score;
explain extended select count (score.score) from score;
select a.bonus from fen_biao_tong.emp a;
select nvl(a.bonus,0) from fen_biao_tong.emp a;
select if(1>2,666,888);
select if(1>0,if(-1>0,000,666),888);
select score ,if(score>95,'优秀',if(score>85,'良好',if(score>60,'及格','不及格')))from score;
select score,
case when score>95 then '优秀'
when score>85 then '良好'
when score>75 then '及格'
else '不及格'
end as `等级`
from score;
select
name,
case name when "施笑槐" then "槐ge"
when "吕金鹏" then "鹏ge"
when "单乐蕊" then "蕊jie"
else "算了,不叫了!!!"
end as `喊你一声你敢答应吗?`
from bigdata17.students2;
select current_date;
select date_sub(current_date,1)
select date_add('2000-01-01',10);
select datediff('2021-06-05','2020-06-04');
select unix_timestamp();
select to_date('2021-06-05 13:34:12');
select from_unixtime(1654521341,'YYYY-MM-dd')
select from_unixtime(unix_timestamp(),'YYYY-MM-dd');
select from_unixtime(unix_timestamp(),'YYYY|MM|dd');
select unix_timestamp('2021年01月14日','yyyy年mm月dd日');
select from_unixtime(unix_timestamp('2021年01月14日','yyyy年mm月dd日'),'yyyy_mm_dd');
select from_unixtime(unix_timestamp("04牛2021数加16逼","mm牛yyyy数加dd逼"),'yyyy-mm-dd');
select concat('123','456');
select concat('woshi','一个','大帅哥!!!')
select concat('123','456',null);
select concat_ws('**','a','v','c');
select concat_ws('+_+','00','^','^','00',null)
select concat_ws("|",cast(id as string),name,cast(age as string),gender,clazz)
from bigdata17.students limit 10;
select substring('abcdefg',1,4);
select concat_ws("-",substring('2021/01/14',1,4),substring('2021/01/14',6,2),substring('2021/01/14',9,2));
select from_unixtime(unix_timestamp('2021/01/14','yyyy/MM/dd'),'yyyy-MM-dd');
select split("sdfs,werw,weger,werwetg,423w,werhe,erw,ewergw,777,777,777,888,88,888,833,33,",",");
select split("sdfs,werw,weger,werwetg,423w,werhe,erw,ewergw,777,777,777,888,88,888,833,33,",",")[3];
select b.a `字段名`,count(b.a) `字段的个数` from (select explode(split("sdfs,werw,weger,werwetg,423w,werhe,erw,ewergw,777,777,777,888,88,888,833,33,",",")) a) b
group by b.a order by `字段的个数`;
create table t_fraction(
name string,
subject string,
score int)
row format delimited fields terminated by ","
lines terminated by '\n';
load data local inpath'/usr/local/soft/atao_file/t_fraction' into table t_fraction;
select * from t_fraction;
select t_fraction.*,sum(score) over () from t_fraction;
select t_fraction.*,min(score) over () from t_fraction;
select t_fraction.*,max(score) over () from t_fraction;
select t_fraction.*,avg(score) over () from t_fraction;
select t_fraction.*,count(score) over () from t_fraction;
select t_fraction.*,lag(score) over () from t_fraction;
select name,subject,score,sum(score) over() as sum1, sum(score) over (partition by name ) as sum2 from t_fraction;
select name,subject,score,sum(score) over() as sum1, sum(score) over (partition by subject ) as sum2 from t_fraction;
select name,subject,score,sum(score) over() as sum1,
sum(score) over (partition by subject ) as sum2,
sum(score) over (partition by subject order by score rows between unbounded preceding and current row ) as sum3
from t_fraction;
select name,subject,score,sum(score) over() as sum1,
sum(score) over (partition by subject ) as sum2,
sum(score) over (partition by subject order by score rows between unbounded preceding and current row ) as sum3,
sum(score) over (partition by subject order by score rows between 1 preceding and current row) as sum4
from t_fraction;
select name,subject,score,sum(score) over() as sum1,
sum(score) over (partition by subject ) as sum2,
sum(score) over (partition by subject order by score rows between unbounded preceding and current row ) as sum3,
sum(score) over (partition by subject order by score rows between 1 preceding and current row) as sum4,
sum(score) over (partition by subject order by score rows between 1 preceding and 1 following) as sum5
from t_fraction;
select name,subject,score,sum(score) over() as sum1,
sum(score) over (partition by subject ) as sum2,
sum(score) over (partition by subject order by score rows between unbounded preceding and current row ) as sum3,
sum(score) over (partition by subject order by score rows between 1 preceding and current row) as sum4,
sum(score) over (partition by subject order by score rows between 1 preceding and 1 following) as sum5,
sum(score) over (partition by subject order by score rows between current row and unbounded following) as sum6
from t_fraction;
create table business
(
name string,
orderdate string,
cost int
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
drop table business;
load data local inpath '/usr/local/soft/atao_file/business.txt' into table business;
select * from business;
select distinct name, date_format(orderdate,'yyyy-MM'),count(*) over ( ) from business where date_format(orderdate,'yyyy-MM') = '2017-04';
select name,orderdate,cost,sum(cost) over(partition by name) from business;
select name,orderdate,sum(cost) over (partition by name order by date_format(orderdate,'yyyy-MM-dd') rows between unbounded preceding and current row ) from business;
select name ,orderdate,lag(orderdate) over (partition by name order by date_format(orderdate,'yyyy-MM-dd')) from business;
select * from (select name,orderdate,cost,ntile(5) over(order by orderdate) sortgroup_num from business) t where t.sortgroup_num=1;
create table score2
(
name string,
subject string,
score int
) row format delimited fields terminated by "\t";
load data local inpath '/usr/local/soft/atao_file/score2' into table score2;
select * from score2;
select name,subject,score,
rank() over (partition by name order by score desc),
dense_rank() over (partition by name order by score desc ),
row_number() over (partition by name order by score desc )
from score2;
create table testArray2(
name string,
weight array<string>
)row format delimited
fields terminated by '\t'
COLLECTION ITEMS terminated by ',';
drop table testarray2 ;
load data local inpath'/usr/local/soft/atao_file/testArray.txt' into table testarray2 ;
select * from testarray2 ;
select name,col1 from testarray2 lateral view explode(weight) t1 as col1;
select explode(weight) from testarray2;
select explode(map('key1',1,'key2',2,'key3',3)) as (key,value)
select key from (select explode(map('key1',1,'key2',2,'key3',3)) as (key,value)) t;
select value from (select explode(map('key1',1,'key2',2,'key3',3)) as (key,value)) t;
select name,col1,col2 from testarray2 lateral view explode(map('key1',1,'key2',2,'key3',3)) t1 as col1,col2;
select name,pos,col1 from testarray2 lateral view posexplode(weight) t1 as pos,col1;
create table testLieToLine(
name string,
col1 int
)row format delimited
fields terminated by '\t';
load data local inpath '/usr/local/soft/atao_file/testLieToLine' into table testLieToLine;
select * from testlietoline
select name,collect_list(col1) from testlietoline group by name;
select name,concat_ws('|',collect_list(cast(col1 as string))) from testlietoline group by name;
select * from f;
create table deal_tb(
id string
,datestr string
,amount string
)row format delimited fields terminated by ',';
load data local inpath '/usr/local/soft/atao_file/deal_tb' into table deal_tb;
select * from deal_tb;
SELECT id, datestr, sum(amount) AS sum_amount
FROM deal_tb
GROUP BY id, datestr
SELECT t1.id AS id, t1.datestr AS datestr, t1.sum_amount AS sum_amount, row_number() OVER (PARTITION BY t1.id ORDER BY t1.datestr) AS rn
FROM (
SELECT id, datestr, sum(amount) AS sum_amount
FROM deal_tb
GROUP BY id, datestr
) t1
SELECT tt1.id AS id, tt1.datestr AS datestr, tt1.sum_amount AS sum_amount
, date_sub(tt1.datestr, tt1.rn) AS grp
FROM (
SELECT t1.id AS id, t1.datestr AS datestr, t1.sum_amount AS sum_amount, row_number() OVER (PARTITION BY t1.id ORDER BY t1.datestr) AS rn
FROM (
SELECT id, datestr, sum(amount) AS sum_amount
FROM deal_tb
GROUP BY id, datestr
) t1
) tt1
SELECT ttt1.id, ttt1.grp
, round(sum(ttt1.sum_amount), 2) AS user_sum_amount
, count(1) AS user_days
, min(ttt1.datestr) AS user_start_date
, max(ttt1.datestr) AS user_end_date
, datediff(ttt1.grp, lag(ttt1.grp, 1) OVER (PARTITION BY ttt1.id ORDER BY ttt1.grp)) AS interval_days
FROM (
SELECT tt1.id AS id, tt1.datestr AS datestr, tt1.sum_amount AS sum_amount
, date_sub(tt1.datestr, tt1.rn) AS grp
FROM (
SELECT t1.id AS id, t1.datestr AS datestr, t1.sum_amount AS sum_amount, row_number() OVER (PARTITION BY t1.id ORDER BY t1.datestr) AS rn
FROM (
SELECT id, datestr, sum(amount) AS sum_amount
FROM deal_tb
GROUP BY id, datestr
) t1
) tt1
) ttt1
GROUP BY ttt1.id, ttt1.grp;
select id,datestr,sum(amount) from deal_tb group by id,datestr ;
select t.id,t.datestr,t.sum,row_number() over (partition by id order by datestr) flag from
(select id,datestr,sum(amount) sum from deal_tb group by id,datestr) t ;
select tt1.id id,tt1.datestr datestr, tt1.sum sum, day(tt1.datestr)-tt1.flag flag from (select t.id id,t.datestr datestr,t.sum sum,
row_number() over (partition by id order by datestr) flag from
(select id,datestr,sum(amount) sum from deal_tb group by id,datestr) t) tt1 ;
select
ttt1.id,
count(1) over (partition by ttt1.id ,ttt1.flag),
min(ttt1.datestr) over (partition by ttt1.id,ttt1.flag order by ttt1.datestr ),
max(ttt1.datestr) over (partition by ttt1.id,ttt1.flag )
from (
select tt1.id id,tt1.datestr datestr, tt1.sum sum, day(tt1.datestr)-tt1.flag flag from (select t.id id,t.datestr datestr,t.sum sum,
row_number() over (partition by id order by datestr) flag from
(select id,datestr,sum(amount) sum from deal_tb group by id,datestr) t) tt1 ) ttt1 ;
========================================================================================
select distinct
tttt1.id,
tttt1.sum_time,
tttt1.start_time,
tttt1.end_time,
lag(tttt1.end_time) over (partition by tttt1.end_time)
from
(
select
ttt1.id id,
count(1) over (partition by ttt1.id ,ttt1.flag) sum_time,
min(ttt1.datestr) over (partition by ttt1.id,ttt1.flag order by ttt1.datestr) start_time,
max(ttt1.datestr) over (partition by ttt1.id,ttt1.flag ) end_time
from (
select tt1.id id,tt1.datestr datestr, tt1.sum sum, day(tt1.datestr)-tt1.flag flag from (select t.id id,t.datestr datestr,t.sum sum,
row_number() over (partition by id order by datestr) flag from
(select id,datestr,sum(amount) sum from deal_tb group by id,datestr) t) tt1 ) ttt1 ) tttt1 order by tttt1.id,tttt1.start_time;
select
distinct *
from
(
select
ttt1.id id,
sum(ttt1.sum) over (partition by ttt1.id,ttt1.flag),
count(1) over (partition by ttt1.id, ttt1.flag),
min(ttt1.datestr) over (partition by ttt1.id, ttt1.flag ) start_time,
max(ttt1.datestr) over (partition by ttt1.id, ttt1.flag),
from
(select tt1.id id,tt1.datestr datestr, tt1.sum sum, day(tt1.datestr)-tt1.flag flag from
(select t.id id,t.datestr datestr,t.sum sum,
row_number() over (partition by id order by datestr) flag from
(select id,datestr,sum(amount) sum from deal_tb group by id,datestr) t) tt1) ttt1 ) tttt1 order by tttt1.id,tttt1.start_time;
select distinct *
from
(
select
ttt1.id id,
sum(ttt1.sum) over (partition by ttt1.id,ttt1.flag),
count(1) over (partition by ttt1.id, ttt1.flag),
min(ttt1.datestr) over (partition by ttt1.id, ttt1.flag) start_time,
max(ttt1.datestr) over (partition by ttt1.id, ttt1.flag),
ttt1.flag,
lag(flag) over (partition by ttt1.id)
from
(select tt1.id id,tt1.datestr datestr, tt1.sum sum, day(tt1.datestr)-tt1.flag flag from
(select t.id id,t.datestr datestr,t.sum sum,
row_number() over (partition by id order by datestr) flag from
(select id,datestr,sum(amount) sum from deal_tb group by id,datestr) t) tt1) ttt1
) tttt1 order by tttt1.id, tttt1.start_time;
create table f
(
date_time string,
cost string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
load data local inpath'/usr/local/soft/atao_file/f' into table f;
select * from f;
select avg(f.cost) over (order by from_unixtime(unix_timestamp(f.date_time,'yyyymmdd'),'yyyy-mm-dd') row 6 perceding) from f;
2018/6/1
select substring(f.date_time,1,4) from f; 2018
select concat('0',substring(f.date_time,6,1)) from f; 6
select concat('0',substring(f.date_time,8,1)) from f; 8
select substring(f.date_time,1,4) from f; 2018
select concat('0',substring(f.date_time,6,1)) from f; 6
select substring(f.date_time,8,2) from f; 8
select LENGTH(f.date_time) from f;
select if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))) from f;
select concat(from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd'),'~',
date_sub(from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd'),6)),
round(avg(f.cost) over (order by from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd') rows 6 preceding),2) from f;
select from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd')
from f;
select date_sub(from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd'),6)
from f;
select concat(from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd'),'~',
date_sub(from_unixtime(unix_timestamp(if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))),'yyyymmdd'),'yyyy-mm-dd'),6)) from f;
select if(LENGTH(f.date_time) = 8,
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),concat('0',substring(f.date_time,8,1))),
concat(substring(f.date_time,1,4),concat('0',substring(f.date_time,6,1)),substring(f.date_time,8,2))) from f;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示