1

我现在有一个问题,我想更新我的表中名为 Service_Log_Temp 的作业的最早条目,重复 = '否',其余的重复 = '是'

这是我现在的桌子:

http://s15.postimage.org/gulifcaiz/Current_Table.jpg

所以我想要一个查看 K_B_Job_No 的脚本(你会看到我在这里列出了 2 个不同的工作)并根据 L1_GA_SR_Creation_Date 和 L2_B_Start_time_Of_Activity 更新最早的条目,其中 Duplicate = 'No',其余的 Duplicate = 'Yes'。

它应该是这样的:

http://s14.postimage.org/h7zx5xgj5/Desired_Table.jpg

有任何想法吗?

4

3 回答 3

0

这应该在postgres中工作,其中连接是|| - 您必须将其更改为系统中的串联

背景:查找每个作业的第一次运行时间:

select k_r_job_no,min( L1_GA_SR_Creation_Date+L2_B_Start_time_Of_Activity) from
service_log_temp
group by k_r_job_no

问题是我们无法从这个查询中得到 ids ......

但我们可以将其用作子查询。由于通常不可能从子查询中获取多于一行,因此我们必须进行串联以获得每个作业的正确时间:(您可能需要进行类型转换才能使串联工作)

select key_service from service_log_temp where 
   k_r_job_no||L1_GA_SR_Creation_Date+L2_B_Start_time_Of_Activity in( 
     select k_r_job_no||min( L1_GA_SR_Creation_Date+L2_B_Start_time_Of_Activity) 
       from service_log_temp
        group by k_r_job_no
     )

这可以重写为更新:

update service_log_temp set Duplicate='Yes' where 
   k_r_job_no||L1_GA_SR_Creation_Date+L2_B_Start_time_Of_Activity in( 
     select k_r_job_no||min( L1_GA_SR_Creation_Date+L2_B_Start_time_Of_Activity) 
       from service_log_temp
        group by k_r_job_no
     )

然后之后

 update service_log_temp set Duplicate='No' where Duplicate is null

(或者,如果您想在一个查询中使用 SRIRAM 建议的“案例”)

于 2012-11-22T10:21:57.737 回答
0
;with a as (select K_B_Job_No, min(L1_GA_SR_Creation_Date + cast(L2_B_Start_time_Of_Activity as datetime)) as dttime from Service_Log_Temp
group by K_B_Job_No)
select key_service,K_B_Job_No,L1_GA_SR_Creation_Date,L2_B_Start_time_Of_Activity, 
(case when
(select dttime from a where a.K_B_Job_No=b.K_B_Job_No)=(L1_GA_SR_Creation_Date + cast(L2_B_Start_time_Of_Activity as datetime))
then 'NO' else 'YES' end) as Duplicate
from Service_Log_Temp b
于 2012-11-22T10:10:14.767 回答
0

尝试这个

select key_service,k_r_job_no,L1_GA_SR_Creation_Date,L2_B_Start_time_Of_Activity,
   case 
    when (select min(L1_GA_SR_Creation_Date) and min(L2_B_Start_time_Of_Activity)
       then 'No'
      else 'Yes'
  end as Duplicate
from Service_Log_Temp
group by k_r_job_no
于 2012-11-22T09:42:24.617 回答