2017-10-05 71 views
-3

我需要從這個輸出..我知道這是錯誤的,這只是一個例子如何添加CASE SELECT語句

SELECT 
    customer_name, 
    ((Qty * 0.5), * 
    (CASE WHEN (DATEDIFF(month, Appointment_Date,getdate())) < 12)'Incentive' 
FROM t1,t2 
WHERE t1.customer_code = t2.customer_code 
+0

解釋你正在嘗試如何實現..讓我們從你的完全錯誤的代碼猜是你和我 –

+0

兄弟我需要的人的輸出中與DATEDIFF個月低於12,所以我浪費時間需要在這裏使用案例 –

+1

爲什麼不是'where'子句? (月,Appointment_Date,getdate())<12' –

回答

0

我認爲你正在尋找這樣的:

select customer_name,((Qty * 0.5) * 
(case when (datediff (month, Appointment_Date,getdate()) < 12) then 
datediff (month, Appointment_Date,getdate()) else 1 end))'Incentive' 
from t1,t2 
where t1.customer_code = t2.customer_code 
0

據我的理解,從你的意見(請閱讀關於如何張貼SO的介紹性文件),也許你可以使用這樣的查詢。

我改變了你原來的查詢使用JOIN子句了。

SELECT 
    customer_name, 
    Qty * 0.5 *DATEDIFF(month, Appointment_Date,getdate()) AS INCENTIVE 
FROM t1 
INNER JOIN t2 ON t1.customer_code = t2.customer_code 
WHERE DATEDIFF(month, Appointment_Date,getdate()) < 12