如何按条件从多个类别中查找日期

时间:2019-05-20 02:35:09

标签: sql presto

我需要根据条件找到不同类别的数据:破碎场,能量和PR。前两个类别很好。数据是一对一的映射。问题是公关类别。此类别中有几个日期。我需要在此类别中找到min(MilestoneDate),但min(MilesotoneDate)必须大于今天并且最接近今天。

Following are table data:

Site   Building       MilestoneID   MilestoneType  MilestoneDate
 AGE     building1     MIC.MS.1000    Breakground    1/1/18
 AGE     building1     MIC.ACT.1000   Energization   2/3/19
 AGE     building1     PTM.B1.1130    PR             2/5/19
 AGE     building1     PTM.B1.1140    PR             3/4/20
 AGE     building1     PTM.B1.1150    PR             12/25/20
 AGE     building1     PTM.B1.1160    PR             5/25/22
 AGE     building2     MIC.MS.1000    Breakground    2/2/19
 AGE     building2     MIC.ACT.1000   Energization   12/5/19
 AGE     building2     PTM.B2.1130    PR             3/5/20
 AGE     building2      PTM.B2.1140   PR             8/10/20
 AGE     building2      PTM.B2.1150   PR             3/4/21
 AGE     building2      PTM.B2.1160   PR             5/4/22
 OCA     building1     MIC.MS.1000    Breakground    4/2/19
 OCA     building1     MIC.ACT.1000   Energization   5/4/20  
 OCA     building1      PTM.B1.1130   PR             6/8/21
 OCA     building1     PTM.B1.1140    PR             12/2/21
 OCA     building1     PTM.B1.1150    PR             3/5/22
 OCA     building1     PTM.B1.1160    PR             12/30/22

与managementview AS (           选择 *           从management_view_nancy m           左联接                     (                            选择 *                            来自(                                          SELECT projectobjectid,                                                 id AS activity_id,                                                 finishdate作为里程碑日期,                                                 NAME AS里程碑                                          来自活动                                          ds =                                                 (                                                        选择最大(ds)                                                        来自活动)                                          全联盟                                          SELECT projectobjectid,                                                   id AS activity_id,                                                   最少(结束日期)AS结束日期,                                                   名称                                          来自活动                                          在哪里输入('LRP-RVA-B3-FS1000',                                                          'LRP-RVA-B3-PR1000',                                                          'LRP-RVA-B3-PR1500')                                          AND projectobjectid = 4908                                          GROUP BY projectobjectid,                                                   ID,                                                   NAME))p           在try_cast(m.objectid为DOUBLE)= p.projectobjection           AND m.id = p.activity_id),pr AS (          选择 *,                   row_number()OVER(按站点,建筑物,里程碑类型的分区ORDER BY try(cast(substr(milestonedate,1,10)AS date)))rn          从管理层的观点          在哪里try(cast(substr(milestonedate,1,10)AS日期))> = CURRENT_DATE          AND trim(milestonetype)='PR') SELECT网站,        建造,        ID,        里程碑类型        里程碑,        里程碑日期 从管理层的观点 里程碑类型IN(“突破地”,                          “激励”) 全联盟 SELECT网站,        建造,        ID,        里程碑类型        里程碑,        里程碑日期 从公关 rn = 1

查询工作正常,但没有得到我想要的结果。我需要PR类别中的min(MilestoneDate),并且min(MilestoneDate)应该大于今天但最接近今天。该查询仍然为我提供了PR类别中的多个MilestoneDate。任何建议将不胜感激

1 个答案:

答案 0 :(得分:0)

我建议使用CTE引入要保留的PR数据点:

WITH cte AS (
    SELECT *,
        ROW_NUMBER() OVER (PARTITION BY Site, Building ORDER BY MilestoneDate) rn
    FROM managementview
    WHERE MilestoneDate >= CURRENT_DATE AND milestonetype = 'PR'
)

SELECT site, building, id, milestonetype, milestone, milestonedate
FROM managementview
WHERE milestonetype IN ('Breakground', 'Energization')
UNION ALL
SELECT site, building, id, milestonetype, milestone, milestonedate
FROM cte
WHERE rn = 1;

如果您的Presto版本不支持CTE,请尝试对其进行内联:

SELECT site, building, id, milestonetype, milestone, milestonedate
FROM managementview
WHERE milestonetype IN ('Breakground', 'Energization')
UNION ALL
SELECT site, building, id, milestonetype, milestone, milestonedate
FROM
(
    SELECT m.*,
        ROW_NUMBER() OVER (PARTITION BY Site, Building ORDER BY MilestoneDate) rn
    FROM managementview m
    WHERE MilestoneDate >= CURRENT_DATE AND milestonetype = 'PR'
) t
WHERE rn = 1;