2012-11-11 80 views
0

我有表TClientsPayment與領域煩惱與插入查詢

idcontract,idclient,last_paid_time,paid,left_to_pay 

,我需要從這個表中插入數據到另一個表TClientsPaymentsHistory與領域

idcontract,idclient,last_paid_time,paid,late 

其中latebit場。 late可以是0或1,這取決於客戶是否支付到25.xx.xxxx。如果沒有late = 1

如果我做一個查詢

insert into TClientPaymentHistory 
select idcontract, 
     idclient, 
     last_paid_time, 
     paid 
from TClientsPayments 

那麼如何設置late場在此插入查詢?

回答

2

使用SELECT子句中的CASE表達,這裏是你如何能做到這樣的僞代碼:

insert into TClientPaymentHistory 
select idcontract, 
     idclient, 
     last_paid_time, 
     paid, 
     CASE 
     WHEN DATEDIFF(day, last_paid_time, 'tell date here') <= something THEN 0 
     ELSE 1 
     END AS late  
from TClientsPayments 

既然你沒有指定你使用的是什麼RDBMS中,你將需要確定根據您使用的SQL方言獲取日期差異的正確方法。

2

這樣的事情,雖然你將需要大約條件更加清晰:

Insert into TClientPaymentHistory (
    idcontract, idclient, last_paid_time, paid, late 
) 
Select 
    idcontract, 
    idclient, 
    last_paid_time, 
    paid, 
    Case When last_paid_time < 25.xx.xxxx then 0 else 1 end 
From 
    TclientPayments