配置单元sql—检索日期范围中存在的列的不同值

o8x7eapl  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(347)

我在 hive 里有一张table,如下所示:

|attribute | start_date | end_date   | 
|----------+------------+------------+ 
| x        | 10-01-2014 | 03-31-2015 | 
| x        | 04-01-2015 | 09-30-2015 | 
| x        | 10-01-2015 | 03-31-2016 |
| x        | 04-01-2016 | 09-30-2016 |
| y        | 10-01-2015 | 03-31-2016 | 
| y        | 04-01-2016 | 09-30-2015 |

我只想得到2014年1月10日至2016年9月30日期间发生的所有属性的不同列表,但它们必须在每个属性中都有开始日期(2014年1月10日、2015年1月4日、2015年1月10日、2016年1月4日)。
我试过:

select distinct(attribute),min(start_date) ,max(end_date) 
from table 
where 
    max(end_date) >='03-31-2016' 
    and min(effective_start_date) <='2015-01-01'

但这并不能阻止任何差距。任何帮助都将不胜感激。

6l7fqoea

6l7fqoea1#

像这样的?

select attribute
from t
where startdate in ('10-01-2014', '04-01-2015', '10-01-2015', '04-01-2016')
having count(distinct date) = 4;

我建议您使用iso标准格式“2014-10-01”存储和引用日期,但我将日期保留为您的格式。

相关问题