SQL - 使用日期链接到2个日期之间'逻辑

时间:2018-02-02 12:39:20

标签: sql presto amazon-athena

SQL - 我的表Values包含值和相应的日期,表Periods包含句点标签和这些句点的开始日期(不规则日期)。

我试图链接两个表,以便为每个具有相应日期的值分配一个与每个期间相关的标签(基于日期检查)。有一个例子:

Periods
--------
Desc  Dates
M1    03/01/2000
M2    05/02/2000
M3    10/03/2000
...
M12   01/12/2000


Values
-------
Value_Date   Value
05/01/2000   101
01/02/2000   201

07/02/2000   301
15/02/2000   401

我想得到这样的东西:

Results_1
----------
Value_Date   Value  Desc
05/01/2000   101    M1
01/02/2000   201    M1
07/02/2000   301    M2
15/02/2000   401    M2

 and then 


Results_2
----------
Desc   Value_Total
M1     302
M2     702

在Athena / Presto SQL中执行此操作的有效方法是什么?

2 个答案:

答案 0 :(得分:0)

您可以加入开始日期小于或等于值日期的所有期间,然后关闭除最新之外的所有期间。这可能效率不高,但可以胜任。

select ranked.desc, sum(value) as total
from
(
  select
    v.value_date,
    v.value,
    p.desc,
    row_number() over (partition by v.value_date, v.value order by p.dates desc) as rn
  from values v
  join periods p on p.dates <= v.value_date
) ranked
where rn = 1
group by ranked.desc
order by ranked.desc;

(如果value_date表中的values是唯一的,那么您可以从分区子句中删除, v.value。)

答案 1 :(得分:0)

您可以使用LEAD来获取期间的结束日期。然后加入很简单。

with complete_periods as
(
  select 
    p.desc,
    p.dates as start_date,
    coalesce(
      lead(p.dates) over (order by start_date) - interval '1' day,
      date '2999-12-31' -- high date, so we don't have to deal with null
    ) as end_date
  from periods p
)
select p.desc, sum(v.value) as total
from values v
join complete_periods p on v.value_date between p.start_date and p.end_date
group by p.desc
order by p.desc;