提供column = value并选择min date列,比较2列

时间:2017-05-23 13:09:31

标签: sql hive hiveql

我有下表

sec_id|pri_id   |date_col|  
abc|    1   |28-02-2017|    
bcd|    1   |09-01-2017|    
ef| 1   |   |
ghi|    2   |04-04-2017|    
jkl|    2   |05-05-2016|

如果sec_id属于同一个pri_id,则选择具有最早日期的sec_id,并将其赋值为1,其他为零。

输出这样的东西 output table

|sec_id |pri_id |date_col  |new_column |
|abc    |1  |28-02-2017|    0|
|bcd    |1  |09-01-2017|    1|
|ef |1  |      |    0|
|ghi    |2  |04-04-2017|    0|
|jkl    |2  |05-05-2016|    1|

select f_id, s_id, case when min(cast(date_id as timestamp)) then 1 else 0 end as new_column from test_sc group by f_id,s_id. 

另外还有其他条件,如果date_col为null则接受col_e ...如果col_e = r然后将1作为新new_col,如果col_e为null,则将col_f ... if col_f置于最低值..类似明智的6个条件。但同时只是date_col。

2 个答案:

答案 0 :(得分:0)

select  *
       ,case 
            when pri_id = min(pri_id) over (partition by sec_id) 
            then 1 
            else 0 
        end as new_column

from    mytable
+-----+--------+------------+------------+
| dt  | sec_id |   pri_id   | new_column |
+-----+--------+------------+------------+
| ef  |      1 | (null)     |          0 |
| bcd |      1 | 2017-01-09 |          1 |
| abc |      1 | 2017-02-28 |          0 |
| jkl |      2 | 2016-05-05 |          1 |
| ghi |      2 | 2017-04-04 |          0 |
+-----+--------+------------+------------+

答案 1 :(得分:-1)

就您的第一个问题而言,您可以尝试以下方法:

SELECT t1.sec_id,
       t1.pri_id,
       t1.date_col,
       CASE WHEN t1.date_col = t2.date_col THEN 1 ELSE 0 END AS new_column
FROM   my_table t1
JOIN   my_table t2
  ON   t1.pri_id = t2.pri_id
 AND   t2.date_col = (SELECT MIN(date_col)
                      FROM   my_table t3
                      WHERE  t2.pri_id = t3.pri_id)