当rn = 2时如何选择min

时间:2017-09-28 16:39:24

标签: sql

我有一个数据库表:

select SiteName, LastName, Id, Type, StartDate, StartTime, Duration, RN

来自mytable

SiteName    LastName    Id      Type    StartDate   StartTime   Duration    RN
--------    --------    --      ----    ---------   ---------   --------    --  
PK          Jones       284     iov     20171026    830         5           1
PK          Jones       284     iov     20171026    845         5           2
PK          Jones       284     iov     20171113    740         5           1
PK          Jones       284     iov     20171113    845         5           2
PK          Jones       284     iov     20171114    855         5           1
PK          Jones       284     iov     20171201    640         5           1
PK          Jones       284     iov     20171201    845         5           2
PK          Jones       284     iov     20171206    840         5           1
PK          Jones       284     iov     20171206    845         5           2
PK          Jones       284     iov     20171208    855         5           1
PK          Jones       284     iov     20171214    840         5           1
PK          Jones       284     iov     20171214    845         5           2
PK          Jones       284     iov     20171215    840         5           1
PK          Jones       284     iov     20171215    845         5           2
PK          Jones       284     iov     20171222    840         5           1

我的查询给了我这些结果:

select SiteName, LastName, Id, Type, StartDate, min(StartTime), Duration
from mytable 
where RN  = 2
group by SiteName, LastName, Id, Type, StartDate,  Duration



SiteName    LastName    Id      Type    StartDate   StartTime   Duration    
--------    --------    --      ----    ---------   ---------   --------        
PK          Jones       284     iov     20171026    845         5       
PK          Jones       284     iov     20171113    845         5       
PK          Jones       284     iov     20171201    845         5       
PK          Jones       284     iov     20171206    845         5       
PK          Jones       284     iov     20171214    845         5       
PK          Jones       284     iov     20171215    845         5       
PK          Jones       284     iov     20171222    845         5

当RN = 2时,我想返回SiteName,LastName,Id,Type,StartDate,min(StartTime),每个StartDate组的持续时间。 例如,我的结果应如下所示:

SiteName    LastName    Id      Type    StartDate   StartTime   Duration    
--------    --------    --      ----    ---------   ---------   --------        
PK          Jones       284     iov     20171026    830         5       
PK          Jones       284     iov     20171113    740         5       
PK          Jones       284     iov     20171201    640         5       
PK          Jones       284     iov     20171206    840         5       
PK          Jones       284     iov     20171214    840         5       
PK          Jones       284     iov     20171215    840         5       
PK          Jones       284     iov     20171222    840         5

1 个答案:

答案 0 :(得分:0)

您可以尝试以下SQL查询。

SELECT SiteName, 
       LastName, 
       Id, 
       Type, 
       StartDate, 
       MIN(StartTime) StartTime, 
       Duration
  FROM mytable
 WHERE StartDate IN (SELECT StartDate
                       FROM mytable
                      WHERE RN = 2)
 GROUP BY SiteName, 
          LastName, 
          Id, 
          Type, 
          StartDate, 
          Duration

Result

SiteName    LastName    Id  Type    StartDate   StartTime   Duration
PK          Jones       284 iov     2017-10-26  830         5
PK          Jones       284 iov     2017-11-13  740         5
PK          Jones       284 iov     2017-12-01  640         5
PK          Jones       284 iov     2017-12-06  840         5
PK          Jones       284 iov     2017-12-14  840         5
PK          Jones       284 iov     2017-12-15  840         5