如何排除最近日期的结果?

时间:2018-04-10 12:36:12

标签: sql

我对SQL非常陌生,并且只是通过谷歌搜索和实验学习。我试图从工作站和作业流列中的重复日期获取所有工作站名称,作业流名称和有效日期。我目前有以下查询:

SELECT   T2.WORKSTATION_NAME,T2.JOB_STREAM_NAME,T2.JOB_STREAM_VALID_FROM
FROM     (SELECT  JOB_STREAM_REFS_V.WORKSTATION_NAME,
                 JOB_STREAM_REFS_V.JOB_STREAM_NAME
          FROM     MDL.JOB_STREAM_REFS_V AS JOB_STREAM_REFS_V
          GROUP BY WORKSTATION_NAME,JOB_STREAM_NAME
          HAVING   COUNT(JOB_STREAM_NAME) > 1
          ORDER BY WORKSTATION_NAME,JOB_STREAM_NAME) T1
JOIN MDL.JOB_STREAM_REFS_V T2 ON T1.WORKSTATION_NAME = T2.WORKSTATION_NAME
                             AND T1.JOB_STREAM_NAME = T2.JOB_STREAM_NAME

这给出了以下结果,这是我所期望的:

WORKSTATION_NAME    JOB_STREAM_NAME     JOB_STREAM_VALID_FROM 
-------------------------------------------------------------
STATION1            STREAMA             2015-04-26 
STATION1            STREAMA             2015-04-27 
STATION2            STREAMB             2016-04-05 
STATION2            STREAMB             2016-07-25 
STATION2            STREAMB             2016-09-05 
STATION2            STREAMB             2017-07-25 
STATION2            STREAMC             2016-09-21 
STATION2            STREAMC             2016-10-21 
STATION3            STREAMD             2016-08-08 
STATION3            STREAMD               
STATION3            STREAME             2016-09-04 
STATION3            STREAME              

但是,我想根据有效的日期排除每个工作站/作业流的最新条目,并且只返回日期中有效期较长或日期无效的结果,因此我只得到以下内容:

WORKSTATION_NAME    JOB_STREAM_NAME     JOB_STREAM_VALID_FROM 
-------------------------------------------------------------
STATION1            STREAMA             2015-04-26 
STATION2            STREAMB             2016-04-05 
STATION2            STREAMB             2016-07-25 
STATION2            STREAMB             2016-09-05 
STATION2            STREAMC             2016-09-21 
STATION3            STREAMD               
STATION3            STREAME              

我现在完全失去了,并且不确定如何处理这个问题。我已经尝试使用MAX来获取最新日期,并且仅显示使用WHERE的所有内容,但我无法使其工作,我不确定在我的查询中将这些语句放在哪里并且有我有点冒险。

2 个答案:

答案 0 :(得分:0)

您可以使用ANSI标准窗口功能执行所需操作。我想这就是你想要的:

SELECT WORKSTATION_NAME, JOB_STREAM_NAME, JOB_STREAM_VALID_FROM
FROM (SELECT jsr.*,
             COUNT(*) OVER (PARTITION BY jsr.WORKSTATION_NAME, jsr.JOB_STREAM_NAME) as cnt,
             MAX(jr.JOB_STREAM_VALID_FROM) OVER (PARTITION BY jsr.WORKSTATION_NAME, jsr.JOB_STREAM_NAME) as max_JOB_STREAM_VALID_FROM
      FROM MDL.JOB_STREAM_REFS_V jsr
     ) jsr
WHERE cnt > 1 AND JOB_STREAM_VALID_FROM < max_JOB_STREAM_VALID_FROM
ORDER BY WORKSTATION_NAME, JOB_STREAM_NAME

答案 1 :(得分:0)

您的子查询为您提供了包含多个条目的workstation_name / job_stream_name对。在此基础上,您可以选择记录。您只需在子查询中选择最大日期,然后在主查询中将其排除。这是您对job_stream_valid_from添加的比较的查询:

select   
  t2.workstation_name,
  t2.job_stream_name,
  t2.job_stream_valid_from
from     
(
  select 
    workstation_name, 
    job_stream_name, 
    max(job_stream_valid_from) as max_job_stream_valid_from
  from mdl.job_stream_refs_v
  group by workstation_name, job_stream_name
  having count(*) > 1
) t1
join mdl.job_stream_refs_v t2 on t1.workstation_name = t2.workstation_name
                             and t1.job_stream_name = t2.job_stream_name
                             and max_job_stream_valid_from > t2.job_stream_valid_from
order by t2.workstation_name, t2.job_stream_name;

您现在甚至可以删除having count(*) > 1,因为无论如何,每个workstation_name / job_stream_name的最后一条记录都会被排除在外。这表明我们可以重新制定任务:给我们所有有更新兄弟的条目。这很简单:

select *
from mdl.job_stream_refs_v jsr
where exists
(
  select *
  from mdl.job_stream_refs_v newer
  where newer.workstation_name = jsr.workstation_name
    and newer.job_stream_name = jsr.job_stream_name
    and newer.job_stream_valid_from > jsr.job_stream_valid_from
)
order by workstation_name, job_stream_name;