如何返回连续2行的最小(时间),其中时间相隔15分钟,按日期分组

时间:2017-10-17 15:03:54

标签: sql sql-server-2008

我在表格中有数据:

ldd /usr/local/lib/libtdsodbc.so
ldd: warning: you do not have execution permission for `/usr/local/lib/libtdsodbc.so'
        linux-vdso.so.1 =>  (0x00007ffe145fe000)
        libodbcinst.so.2 => /lib64/libodbcinst.so.2 (0x00007f81f9dfd000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f81f9bf8000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f81f99dc000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f81f961b000)
        libltdl.so.7 => /usr/local/lib/libltdl.so.7 (0x00007f81f940f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f81fa2ac000)

我试图返回连续2行的最小值(时间),时间相隔15分钟,按日期分组

例如,我的结果应如下所示:

select SiteName, LName, Loc1, Appt, Date, Time, Dur from appt 

SiteName    LName   Loc1    Appt    Date        Time    Dur RN
Main Office Jones   MO      Annual  20171018    1330    15  1
Main Office Jones   MO      Annual  20171018    1345    15  2


Main Office Jones   MO      Annual  20171018    1530    15  3
Main Office Jones   MO      Annual  20171018    1545    15  4


Main Office Jones   MO      Annual  20171024    1045    15  1-----I don't need this row


Main Office Jones   MO      Annual  20171024    1530    15  2
Main Office Jones   MO      Annual  20171024    1545    15  3


Main Office Jones   MO      Annual  20171025    1330    15  1
Main Office Jones   MO      Annual  20171025    1345    15  2


Main Office Jones   MO      Annual  20171025    1430    15  3
Main Office Jones   MO      Annual  20171025    1445    15  4


Main Office Jones   MO      Annual  20171025    1530    15  5
Main Office Jones   MO      Annual  20171025    1545    15  6


Main Office Jones   MO      Annual  20171027    1045    15  1-----I don't need this row

Main Office Jones   MO      Annual  20171027    1530    15  2
Main Office Jones   MO      Annual  20171027    1545    15  3


Main Office Jones   MO      Annual  20171031    1030    15  1
Main Office Jones   MO      Annual  20171031    1045    15  2


Main Office Jones   MO      Annual  20171031    1330    15  3
Main Office Jones   MO      Annual  20171031    1345    15  4


Main Office Jones   MO      Annual  20171031    1430    15  5
Main Office Jones   MO      Annual  20171031    1445    15  6


Main Office Jones   MO      Annual  20171031    1530    15  7
Main Office Jones   MO      Annual  20171031    1545    15  8


Main Office Jones   MO      Annual  20171101    1145    15  1

Main Office Jones   MO      Annual  20171101    1330    15  2
Main Office Jones   MO      Annual  20171101    1345    15  3

Main Office Jones   MO      Annual  20171101    1430    15  4
Main Office Jones   MO      Annual  20171101    1445    15  5

这是我到目前为止的查询,但我需要额外的逻辑来显示连续2行的最小(时间),其中时间相隔15分钟,按日期分组

Results:

SiteName    LName   Loc1    Appt    Date        Time    Dur 
Main Office Jones   MO      Annual  20171018    1330    15  
Main Office Jones   MO      Annual  20171018    1530    15  
Main Office Jones   MO      Annual  20171024    1530    15  
Main Office Jones   MO      Annual  20171025    1330    15  
Main Office Jones   MO      Annual  20171025    1430    15  
Main Office Jones   MO      Annual  20171025    1530    15  
Main Office Jones   MO      Annual  20171027    1530    15  
Main Office Jones   MO      Annual  20171031    1030    15  
Main Office Jones   MO      Annual  20171031    1330    15  
Main Office Jones   MO      Annual  20171031    1430    15  
Main Office Jones   MO      Annual  20171031    1530    15  
Main Office Jones   MO      Annual  20171101    1330    15  
Main Office Jones   MO      Annual  20171101    1430    15  

一切正常,除非我在表格中遇到这样的数据

    select t.SiteName,  t.LName,    t.Loc1, t.Appt, t.Date,  min(t.Time) as Time, Dur
    from appt t
        inner join (select Date,
                       max(rn) rn_x
                from appt
                where rn % 2 = 0 
                group by Date ) x
        on t.Date = x.Date
       and t.rn <= x.rn_x
    group by t.SiteName,    t.LName,    t.Loc1, t.Appt, t.Date,  t.Dur,
           case when rn%2 = 0
                    then rn/2 - 1
           else rn/2
           end

结果正在返回

 select SiteName, LName, Loc1, Appt, Date, Time, Dur from appt 

 SiteName        LName      Loc1    Appt        Date        Time    Dur 
 Main Office    Jones       MO  Office Visit    20171108    1300    15  
 Main Office    Jones       MO  Office Visit    20171108    1315    15  
 Main Office    Jones       MO  Office Visit    20171108    1330    15  
 Main Office    Jones       MO  Office Visit    20171108    1345    15

1 个答案:

答案 0 :(得分:1)

我不确定您为什么使用rn来获取结果,因为它似乎与日期和时间列无关。如果您希望获得间隔15分钟的行,最佳选择是将datetime列合并到一个datetime列中,以便您处理完整日期。

此外,我不知道date / timedate / time类型还是仅char列,所以我是将两个版本放在一起:

分别为date / time

date / time

with Appointments_DT as (
  select *, cast([date] as datetime)+cast([time] as datetime) as AppDatetime
  from Appointments
)
select SiteName, LName, Loc1, Appt, [Date], [Time], Dur
from Appointments_DT as ADT1
where exists (select * from Appointments_DT AS ADT2
  where ADT2.SiteName=ADT1.SiteName
    and ADT2.LName=ADT1.LName
    and ADT2.Loc1=ADT1.Loc1
    and ADT2.Appt=ADT1.Appt
    and ADT2.AppDatetime=dateadd(minute,15,ADT1.AppDatetime))

SQL fiddle

分别为date / time

char(8) / char(4)

with Appointments_DT as (
  select *, cast([date]+' '+left([time],2)+':'+right([time],2) as datetime) as AppDatetime
  from Appointments
)
select SiteName, LName, Loc1, Appt, [Date], [Time], Dur
from Appointments_DT as ADT1
where exists (select * from Appointments_DT AS ADT2
  where ADT2.SiteName=ADT1.SiteName
    and ADT2.LName=ADT1.LName
    and ADT2.Loc1=ADT1.Loc1
    and ADT2.Appt=ADT1.Appt
    and ADT2.AppDatetime=dateadd(minute,15,ADT1.AppDatetime))

SQL fiddle

在这两个版本中,我们使用EXISTS来获取具有相同位置和类型的另一行的行,但恰好在15分钟之后。