如何选择并插入另一个临时表

时间:2017-03-04 08:50:19

标签: sql sql-server stored-procedures

   ID   LogDate       LogTime         Mode  

   1    2017-02-23  19:30:00.0000000    1   
   4    2017-02-24  08:00:00.0000000    0   

我得到了上述结果。

我需要得到如下输出

   LogDate        InTime               OutTime

   2017-02-23     19:30:00.0000000    08:00:00.0000000

更新了问题 实际上下面的表是原始表。从这个表我需要得到第一个和最后一个记录,并将第一个logtime显示为InTime,将第二个logtime显示为OutTime作为输出

   ID   LogDate     LogTime        InoutMode    
    1   2017-02-23  19:30:00.0000000    1   
    2   2017-02-23  23:00:00.0000000    0   
    3   2017-02-23  23:30:00.0000000    1   
    4   2017-02-24  08:00:00.0000000    0   

2 个答案:

答案 0 :(得分:1)

使用此

http://sqlfiddle.com/#!6/c74ea/5

WITH cte AS
  (SELECT t.*,
          row_Number() over (
                             ORDER BY logdate,logtime) AS inc,
          row_Number() over (
                             ORDER BY logdate DESC,logtime DESC) AS dec
   FROM table1 t)
SELECT t1.logdate,
       t1.logtime AS intime,
       t2.logtime AS outtime
FROM
  (SELECT logdate,
          logtime,
          inc
   FROM cte
   WHERE inc=1) t1
INNER JOIN
  (SELECT logdate,
          logtime,dec
   FROM cte
   WHERE dec=1) t2 ON t1.inc=t2.dec

答案 1 :(得分:0)

你需要这个选择的准备键 但是

from datetime import datetime, timedelta
current_time = datetime.now().time()
new_time = current_time - timedelta(seconds=10)
>> TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.timedelta'