如何在SELECT语句中添加CASE

时间:2017-10-05 05:03:58

标签: sql-server

我需要这个输出..我知道这是错的它只是一个例子

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

据我所知(请阅读有关如何在S.O.上发布的介绍性说明),也许你可以使用这样的查询。

我也使用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
相关问题