1. 程式人生 > >hive中if和coalesce 去除null值,case when

hive中if和coalesce 去除null值,case when

ref http://blog.csdn.net/mtj66/article/details/52629876

###################################### if usage

select * from (select *,if (b.name is null ,true,false ) as bo from test1 a  left join test2 b on a.name =b.name ) t3;


t3.name t3.age  t3._col2        t3.id   t3.bo
lucy    18      NULL    NULL    true
lily    10      lily    1       false

jim     16      NULL    NULL    true
henry   19      NULL    NULL    true
Time taken: 11.266 seconds, Fetched: 4 row(s)


####################################### COALESCE usage
select * from (select *, COALESCE(b.name is null ,true ) as bool from test1 a  left join test2 b on a.name =b.name ) t3;
OK
t3.name t3.age  t3._col2        t3.id   t3.bool

lucy    18      NULL    NULL    true
lily    10      lily    1       false
jim     16      NULL    NULL    true
henry   19      NULL    NULL    true
Time taken: 10.651 seconds, Fetched: 4 row(s)




CONDITIONAL FUNCTIONS IN HIVE
Hive supports three types of conditional functions. These functions are listed below:


IF( Test Condition, True Value, False Value ) 
The IF condition evaluates the “Test Condition” and if the “Test Condition” is true, then it returns the “True Value”. Otherwise, it
 returns the False Value.
Example: IF(1=1, 'working', 'not working') returns 'working'

COALESCE( value1,value2,... )


The COALESCE function returns the fist not NULL value from the list of values. If all the values in the list are NULL, then it retur
ns NULL. 
Example: COALESCE(NULL,NULL,5,NULL,4) returns 5

CASE Statement 

The syntax for the case statement is:
CASE  [ expression ]
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ...
  WHEN conditionn THEN resultn
  ELSE result
END
Here expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition1, condition2, ... cond
itionn).


All the conditions must be of same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the
 case statement will return the result and not evaluate the conditions any further.


All the results must be of same datatype. This is the value returned once a condition is found to be true.


IF no condition is found to be true, then the case statement will return the value in the ELSE clause. If the ELSE clause is omitted
 and no condition is found to be true, then the case statement will return NULL


Example:


CASE Fruit
  WHEN 'APPLE' THEN 'The owner is APPLE'
  WHEN 'ORANGE' THEN 'The owner is ORANGE'
  ELSE 'It is another Fruit'
END
The other form of CASE is


CASE
  WHEN Fruit = 'APPLE' THEN 'The owner is APPLE'
  WHEN Fruit = 'ORANGE' THEN 'The owner is ORANGE'
  ELSE 'It is another Fruit'

END

SELECT  SUM(population),
CASE country
WHEN '中國'     THEN '亞洲'
WHEN '印度'     THEN '亞洲'
WHEN '日本'     THEN '亞洲'
WHEN '美國'     THEN '北美洲'
WHEN '加拿大'  THEN '北美洲'
WHEN '墨西哥'  THEN '北美洲'
ELSE '其他' END
FROM    Table_A
GROUP BY CASE country
WHEN '中國'     THEN '亞洲'
WHEN '印度'     THEN '亞洲'
WHEN '日本'     THEN '亞洲'
WHEN '美國'     THEN '北美洲'
WHEN '加拿大'  THEN '北美洲'
WHEN '墨西哥'  THEN '北美洲'
ELSE '其他' END;

同樣的,我們也可以用這個方法來判斷工資的等級,並統計每一等級的人數。SQL程式碼如下; 
SELECT
CASE WHEN salary <= 500 THEN '1'
WHEN salary > 500 AND salary <= 600  THEN '2'
WHEN salary > 600 AND salary <= 800  THEN '3'
WHEN salary > 800 AND salary <= 1000 THEN '4'
ELSE NULL END salary_class,
COUNT(*)
FROM    Table_A
GROUP BY
CASE WHEN salary <= 500 THEN '1'
WHEN salary > 500 AND salary <= 600  THEN '2'
WHEN salary > 600 AND salary <= 800  THEN '3'
WHEN salary > 800 AND salary <= 1000 THEN '4'
ELSE NULL END;

相關推薦

hiveifcoalesce 去除null,case when

ref http://blog.csdn.net/mtj66/article/details/52629876 ###################################### if usage select * from (select *,if (b.nam

Hiveif函式Mysqlifnull的轉換

1.在mysql中,ifnull函式的用法,其表示式如下:     IFNULL(expr1,expr2)     如果 expr1 不是 NULL,IFNULL() 返回 expr1,否則它返回 expr2。IFNULL()返

mysqlifnullhiveif函式的轉換

先說說,在mysql中,ifnull函式的用法,其表示式如下:IFNULL(expr1,expr2)如果 expr1 不是 NULL,IFNULL() 返回 expr1,否則它返回 expr2。IFNULL()返回一個數字或字串值,取決於它被使用的上下文環境。舉個應用場景,比

ORACLENVLlCOALESCE的區別

oracl 第一個 ssi 必須 如果 不一致 miss 兩個 oal nvl(COMMISSION_PCT,0)如果第一個參數為null,則返回第二個參數如果第一個參數為非null,則返回第一個參數 COALESCE(EXPR1,EXPR2,EXPR3...EXPRn)從

Spark SQLDataframe join操作含null的列

dataframe util pre table log n-n dram blog between 當在Spark SQL中對兩個Dataframe使用join時,當作為連接的字段的值含有null值。由於null表示的含義是未知,既不知道有沒有,在SQL中null值與任何

python ifelif的區別

pre else bsp 判斷 nbsp 優秀 自動 滿足 lse 如果程序中判斷事件很多,全部用if的話,會遍歷整個程序,用elif 程序運行時,只要if或後續某一個elif之一滿足邏輯值為True,則程序執行完對應輸出語句後自動結束該輪if-elif(即不會再去冗余地執

Sparkrepartitioncoalesce的用法

repartition(numPartitions:Int):RDD[T]和coalesce(numPartitions:Int,shuffle:Boolean=false):RDD[T] 他們兩個都是RDD的分割槽進行重新劃分,repartition只是coalesce介

元件ElementRadioSelect繫結的屬性區分

Radio和Select需要區分以下的是: 在Radio中: 繫結的值是label接收的 <div class="row"> <div class="col-sm-6

根據list物件某個欄位去除重複

現象: 當集合中是物件的時候,因為每個物件都是new出來的,所以無法向字串那樣的去除重複的值。 方法: 1:需要在物件的實體類bean裡面重寫public boolean equals(Objec

Android Service Activity之間傳。(涉及BroadCast的基本用法)

首先先建立一個Android工程(名字自定義)這裡我命名為MyActivity 包名為:package org.hm.myactivity; 再最後給自己的activity命名(名字自定義)此處我命名為MyTestActivity public class MyTestA

HIVELIKERLIKE的區別

看書的過程中發現hive有兩個用於通配的操作符,LIKE和RLIKE,查了些資料總結一點二者的區別 LIKE 語法格式為A [NOT] LIKE B,B是sql下的簡單正則表示式,也叫萬用字元模式,如_匹配一個字元,%可以匹配任意多個字元,A會對錶達式B做匹

HivedistinctGroup by效率對比及處理方式

select res.flag AS flag ,res.source AS source ,res.template AS template ,SUM(res.click_user)

Hive建立呼叫儲存過程及自定義函式

前面的文章《在Hive中實現儲存過程–HQL/SQL》中介紹瞭如何使用HPL/SQL在Hive中實現儲存過程,執行類似Oracle PL/SQL的功能。 一般的業務場景是資料開發人員開發好一個儲存過程,然後週期性的呼叫,傳入不同的引數即可。 本文繼續介紹如何在Hive中利

springmvc返回json資料去除null

package com.xilehang.kaola.po.orderConfirm; import com.fasterxml.jackson.databind.annotation.JsonSerialize; //轉json是排除null @SuppressWar

HiveUDFUDAF的使用

UDF使用者自定義函式(user defined function)–針對單條記錄。 建立函式流程 1、自定義一個Java類2、繼承UDF類 3、重寫evaluate方法 4、打成jar包 6、在hive執行add jar方法7、在hive執行建立模板函式 8、hql中使用D

hive 日期轉換】Hiveyyyymmddyyyy-mm-dd日期之間的切換

方法1: from_unixtime+ unix_timestamp --20171205轉成2017-12-05 select from_unixtime(unix_timestamp('20171

javafloatdouble的取範圍

float:4位元組(32bit),IEEE 754. 範圍:[-3.40282346638528860e+38 , -1.40129846432481707e-45] ∪ [1.40129846432481707e-45 ~ 3.40282346638528860e+38]

hiveUDFUDAF使用說明

Hive進行UDF開發十分簡單,此處所說UDF為Temporary的function,所以需要hive版本在0.4.0以上才可以。 一、背景:Hive是基於Hadoop中的MapReduce,提供HQL查詢的資料倉庫。Hive是一個很開放的系統,很多內容都支援使用者定製,包括

深入理解Verilog HDL阻塞非阻塞賦的不同

非阻塞賦值操作符用小於等於號 (即 <= )表示。在賦值操作時刻開始時計算非阻塞賦值符的RHS表示式,賦值操作時刻結束時更新LHS。在計算非阻塞賦值的RHS表示式和更新LHS期間,其他的Verilog語句,包括其他的Verilog非阻塞賦值語句都能同時計算RHS表示式和更新LHS。非阻塞賦值允許其他的

shell if else 用法詳解

基本語法 shell的if語法和C語言等高階語言非常相似,唯一需要注意的地方就是shell的if語句對空格方面的要求比較嚴格(其實shell對所有語法的空格使用都比較嚴格),如果在需要空格的地方沒有打上空格,都會報錯。如if [ $1x == "ip"x ];then ec