关于时间过滤sql表

时间:2017-03-01 13:47:56

标签: sql-server

我在表格中有以下列

ISOCardNumber    Terminalno.    Eventdatetime
456                 11        10/02/2017 7:12
789                 13        10/02/2017 8:12
456                 22        11/02/2017 11:13
123                 11        10/02/2017 9:59
789                 24        12/02/2017 9:12
123                 22        10/02/17 11:59

isocardnumber在表格中重复两次,并首先与terminalno链接。有奇数(限于11,13),然后它将连接到偶数(限于22,24)。结果需要根据时间进行过滤。 就像我想知道ISOcardnumber10/02/2017 07:12

中有多少11/02/2017 11:13进出?

我尝试了以下查询

SELECT ISOcardNumber, 
       MAX(CASE WHEN TerminalNumber IN (11) THEN TerminalNumber END) AS TerminalNumber_in, 
       MAX(CASE WHEN TerminalNumber IN (11) THEN EventDateTime END) AS Time_in, 
       MAX(CASE WHEN TerminalNumber IN (22, 24) THEN TerminalNumber END) 
                      AS TerminalNumber_out, 
       MAX(CASE WHEN terminalNumber IN (22, 24) THEN Eventdatetime END) AS time_out 
FROM   dbo.vEvents
WHERE  (TerminalNumber IN (11, 22, 24)) AND EventDateTime>='10-2-2017' AND
       EventDateTime <'12-2-2017' 
     AND (CAST(EventDateTime AS TIME) between '07:12' and '23:59')     
GROUP BY ISOcardNumber
ORDER BY Time_in DESC

但我无法过滤很长一段时间,例如从10/02/2017 07:1211/02/2017 10:00

获得以下结果

TicketNumber    Machineno.   TimeIn             Machineno.   Timeout
456                 11       10/02/2017 7:12    22           11/02/2017 11:13
123                 11       10/02/2017 9:59    22           10/02/2017 11:59

1 个答案:

答案 0 :(得分:1)

您不需要单独的谓词来检查日期时间。这应该做:

severity

Demo here