Hive查询以按年份和ID获取日期列组的最小值和最大值

时间:2018-01-30 04:08:06

标签: mysql sql oracle hive hiveql

我有两列的表,id和date。我想获得该年特定id的最大和最小日期。以下是我想要的样本数据和结果 样本数据

id  date_col

123 2015-05-01 04:00:00

123 2017-04-01 04:00:00

123 2017-09-01 04:00:00

123 2014-09-01 04:00:00

123 2012-12-01 05:00:00

123 2016-08-01 04:00:00

123 2014-05-01 04:00:00

123 2016-10-01 04:00:00

我期待的结果

123 2014-05-01   2014-09-01

123 2015-05-01

123 2016-08-01   2016-10-01

123 2017-04-01   2017-09-01

我尝试使用以下多个查询,但我不会按照年份进行排序

SELECT id,MAX(date_col) AS maxdate_col,MIN(date_col) AS mindate_col FROM table GROUP BY id 

SELECT id,MAX(date_col) AS max_votes,MIN(date_col) AS mindate_col,YEAR(date_col) FROM test GROUP BY id,YEAR(date_col) 

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT id
        ,MIN(date_col) AS mindate_col
        ,(CASE WHEN MAX(date_col) <> MIN(date_col) THEN MAX(date_col) END)  AS maxdate_col
        ,YEAR(date_col) AS year_col
FROM    test 
GROUP BY id,YEAR(date_col)
ORDER BY id,year_col

如果您只想要date部分,则可以将其CAST(date_col AS DATE)