SQL创建临时表还是更新表字段?

时间:2015-12-15 16:46:54

标签: sql-server sql-update

我已经从各种连接表中创建了一个新表,其中包含一些逻辑,以根据以下两个时间戳确定某些处理时间。

select  t1.date_created,
        t1.date_due,
        (t1.date_closed - t1.date_created)*86400 as due_time
        t2.date_created.
        t2.date_closed,
        (t2.date_closed - t2.date_created)*86400 as process_time
from table1 as t1
inner join table2 on t1.ID = T2.table1ID

请记住,我已经做了几个连接(只是一个例子)。 现在我想确定t2处理时间是否大于t1时间,如下所示。

case   
  when t1.due_time > t2.process_time
  then 1
  else 0
end as ind_within_due_time

现在我的问题是最佳做法是什么。

一个。使用整个连接逻辑创建临时表。然后创建一个新表来确定ind_within_due_time并删除旧表。

或B.用连接逻辑创建一个表并在表中创建一个空列然后写一个更新脚本以便在之后分配值?

1 个答案:

答案 0 :(得分:0)

为什么你不能这样做

SELECT date_created, 
       date_due, 
       due_time date_created. date_closed, 
       process_time, 
       CASE 
              WHEN t1.due_time > t2.process_time THEN 1 
              ELSE 0 
       END AS ind_within_due_time 
FROM   (SELECT     t1.date_created, 
                   t1.date_due, 
                   (t1.date_closed - t1.date_created)*86400 AS due_time t2.date_created. t2.date_closed,
                   (t2.date_closed - t2.date_created)*86400 AS process_time
        FROM       table1                                   AS t1 
                  INNER JOIN table2 
                  ON t1.id = t2.table1id) a

或同样选择你可以做到这一点

SELECT 
   CASE 
       WHEN (t1.date_closed - t1.date_created)*86400 > (t2.date_closed - t2.date_created)*86400 THEN 1
       ELSE 0 
   END AS ind_within_due_time, 
   t1.date_created, 
   t1.date_due, 
   (t1.date_closed - t1.date_created)*86400 AS due_time t2.date_created. t2.date_closed, 
   (t2.date_closed - t2.date_created)*86400 AS process_time
...