0

我有一个 SQL 表,我试图选择过去一个季度每个站点的最高值,如果没有,则为过去 4 个季度内的最新值。

MaxOfDate   sys_loc_code    chemical_name       Result    
------------------------------------------------------
    6/23/2020   ALTAIR 1    Tetrachloroethene   NULL 
    12/23/2019  ALTAIR 1    Tetrachloroethene   2.2    
    6/10/2019   ALTAIR 10   Tetrachloroethene   1.8
    6/8/2019    ALTAIR 10   Tetrachloroethene   5.5    
    4/24/2019   ALTAIR 10   Tetrachloroethene   3.0    
    6/24/2020   ALTAIR 1S   Tetrachloroethene   NULL
    6/24/2020   ALTAIR 2    Tetrachloroethene   0.5 
    3/18/2020   ALTAIR 2    Tetrachloroethene   5.0 
    6/24/2020   ALTAIR 3    Tetrachloroethene   1.5  
    3/24/2020   ALTAIR 3    Tetrachloroethene   0.5    
    9/17/2019   ALTAIR 4    Tetrachloroethene   1.2 
    5/5/2019    ALTAIR 4    Tetrachloroethene   3.5
    4/18/2019   ALTAIR 4    Tetrachloroethene   2.8
    6/23/2020   ALTAIR 5    Tetrachloroethene   4.7
    3/5/2020    ALTAIRN5    Tetrachloroethene   7.4
    12/17/2019  ALTAIR 5    Tetrachloroethene   2.9
    9/11/2019   ALTAIR 5    Tetrachloroethene   8.5
    6/24/2020   ALTAIR 6    Tetrachloroethene   NULL
    6/23/2020   ALTAIR 7    Tetrachloroethene   NULL  
    9/11/2019   ALTAIR 7    Tetrachloroethene   1.8
    6/24/2020   ALTAIR 8D   Tetrachloroethene   NULL
    6/24/2020   ALTAIR 8S   Tetrachloroethene   3.1
    3/4/2020    ALTAIR 8S   Tetrachloroethene   2.4
    12/17/2019  ALTAIR 8S   Tetrachloroethene   NULL
    9/10/2019   ALTAIR 8S   Tetrachloroethene   1.2
    6/23/2019   ALTAIRN 9D  Tetrachloroethene   NULL   
    5/11/2019   ALTAIRN 9D  Tetrachloroethene   1.0    
    6/24/2020   ALTAIR 9S   Tetrachloroethene   NULL   
    11/13/2019  SSFSDW 309  Tetrachloroethene   20     
    10/18/2019  SSFSMW 202  Tetrachloroethene   NULL   
    10/14/2019  SSFSMW 204  Tetrachloroethene   1.2    
    10/15/2019  SSFSMW 205  Tetrachloroethene   64     
    10/23/2019  SSFSMW 207  Tetrachloroethene   24     
    11/4/2019   SSFSMW 212  Tetrachloroethene   47    

我的预期结果是:

----------------------------------------------------------------------
    6/23/2020   ALTAIR 1    Tetrachloroethene   NULL   Highest Q  
    6/10/2019   ALTAIR 10   Tetrachloroethene   1.8    Most recent after Q2
    6/24/2020   ALTAIR 1S   Tetrachloroethene   NULL   Highest Q2
    3/18/2020   ALTAIR 2    Tetrachloroethene   5.0    Highest Q2    
    6/24/2020   ALTAIR 3    Tetrachloroethene   1.5    Highest Q2
    9/17/2019   ALTAIR 4    Tetrachloroethene   1.2    Most recent after Q2
    3/5/2020    ALTAIRN5    Tetrachloroethene   7.4    Highest Q2
    6/24/2020   ALTAIR 6    Tetrachloroethene   NULL   Highest Q2
    6/23/2020   ALTAIR 7    Tetrachloroethene   NULL   Highest Q2
    6/24/2020   ALTAIR 8D   Tetrachloroethene   NULL   Highest Q2
    6/24/2020   ALTAIR 8S   Tetrachloroethene   3.1    Highest Q2
    6/23/2019   ALTAIRN 9D  Tetrachloroethene   NULL   Most recent after Q2
    5/11/2019   ALTAIRN 9D  Tetrachloroethene   1.0    Most recent after Q2
    6/24/2020   ALTAIR 9S   Tetrachloroethene   NULL   Highest Q2
    11/13/2019  SSFSDW 309  Tetrachloroethene   20     Highest Q2
    10/18/2019  SSFSMW 202  Tetrachloroethene   NULL   Highest Q2
    10/14/2019  SSFSMW 204  Tetrachloroethene   1.2    Highest Q2
    10/15/2019  SSFSMW 205  Tetrachloroethene   64     Highest Q2
    10/23/2019  SSFSMW 207  Tetrachloroethene   24     Highest Q2
    11/4/2019   SSFSMW 212  Tetrachloroethene   47     Highest Q2

每个 应该只有一个记录sys_loc_code。“ALTAIRN 5”结果是 7.4 是我想要的这个站点的唯一值。这是上一季度(Q2)的最高值,尽管后来进行了读数。“ALTAIR 4”应该是 2.8,因为它是自上个季度没有读取数据以来的最新值。如果它比最近一个季度更早,我想要最近的值,而不是最高的。

查询应该是这样的,对于每个唯一的 sys_loc_code,如果日期在上一季度(Q2),给我最高值。否则给我最新的价值。因此,每个唯一的 sys_loc_code 有一条记录,按优先顺序排列,1. 上一季度的最高值或 2. 如果上一季度不存在,则为最近日期。

4

1 回答 1

0

嗯。. . 这是你想要的吗?

select oar.*
from (select oar.*,
             row_number() over (partition by oar.chemical_name, oar.sys_loc_code order by oar.result_value desc) as seqnum
      from [GMP].[GMP_Other_Agencies_Results] oar
      where oar.chemical_name = 'Tetrachloroethene' and
            oar.PrepDate >= datefromparts(year(getdate()) - 1, (month(getdate()) + 2) / 3, 1)
     ) oar
where seqnum = 1;

是一个 db<>fiddle。

于 2020-09-24T03:04:03.297 回答