hive sql - 数据问题 - case语句 - add_month

时间:2017-06-06 14:01:12

标签: hadoop hive hiveql

我必须为load_date中的1yr的每个月的最后一天总结col_n。

查询1:

  select 
     id1,id2,date_col,
    sum(
    CASE
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-11)) then     col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-10)) then     col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-9)) then     col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-8)) then     col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-7)) then col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-6)) then col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-5)) then     col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-4)) then     col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-3)) then col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-2)) then col_n
    WHEN to_date(table_1.date_col) = to_date(add_months('2016-03-31',-1)) then col_n
    WHEN to_date(table_1.date_col) = to_date('2016-03-31') then col_n
END  
    ) as col_n_sum  
    from table_1 table_1 
    where id2=7 and id1 = 2
    group by  id1,id2 ,date_col

output of query 1

查询2:只需从table -without case语句中选择:output of select query

问题:为什么它给4月份的null,6月....虽然有数据在表中所有月份。

如果我硬编码4月然后我得到数据 - 查询 -

CASE WHEN to_date(table_1.date_col) = to_date('2015-04-30') then col_n .

even -Query -

select * from table_1 where (to_date(table_1.date_col) = to_date(add_months('2016-03-31',-11)) ) and  id1 = 2 and id2 = 7 giving data . 

Add_month无法处理案例或我遗漏的内容。

我正在使用Clouder HUE -Hive。

0 个答案:

没有答案