如果在SQL查询中ELSE

时间:2017-02-27 12:43:15

标签: sql ms-access if-statement

我想知道此查询中的错误。事实上我有3个表,我的想法是我有4个日期date_vesting_1到date_vesting_4,我应该检查上次完成的日期以与另一个日期进行对比。所以我在查询中使用If ELSE从日期4到日期1开始,但每次执行都会崩溃。

提前谢谢。

   IF ( (SELECT (c.Date_vesting_4) FROM D_plan_characteristics AS c ) is NOT NULL) 
SELECT 
             e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM 
             R_employee As e INNER JOIN 
                                                            (
                                                            D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p 
                                                                                            ON c.charac_plan_id = p.charac_plan_id
                                                            ) 
                    ON e.emp_id = p.emp_id
WHERE 
             ( (c.date_cessibility) < (c.Date_vesting_4) )
    ELSE IF ( (SELECT (c.Date_vesting_3) FROM D_plan_characteristics As c ) is NOT NULL) 
SELECT 
             e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM 
             R_employee As e INNER JOIN 
                                                            (
                                                            D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p 
                                                                                            ON c.charac_plan_id = p.charac_plan_id
                                                            ) 
                    ON e.emp_id = p.emp_id
WHERE 
             ( (c.date_cessibility) < (c.Date_vesting_3) ) 
   ELSE IF ((SELECT (c.Date_vesting_2) FROM D_plan_characteristics As c ) is NOT NULL) 
SELECT 
             e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM 
             R_employee As e INNER JOIN 
                                                            (
                                                            D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p 
                                                                                            ON c.charac_plan_id = p.charac_plan_id
                                                            ) 
                    ON e.emp_id = p.emp_id
WHERE 
             ( (c.date_cessibility) < (c.Date_vesting_2) ) 
    ELSE IF ((SELECT (c.Date_vesting_1) FROM D_plan_characteristics As as c ) is NOT NULL) 
SELECT 
             e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM 
             R_employee As e INNER JOIN 
                                                            (
                                                            D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p 
                                                                                            ON c.charac_plan_id = p.charac_plan_id
                                                            ) 
                    ON e.emp_id = p.emp_id
WHERE 
             ( (c.date_cessibility) < (c.Date_vesting_1) );

3 个答案:

答案 0 :(得分:1)

我认为使用coalesce只使用一个查询更简单:

SELECT e.emp_id,
       e.emp_lname,
       e.emp_fname,
       e.effective_date AS eff_date
FROM   r_employee AS e
       INNER JOIN ( d_plan_characteristics AS c
                    INNER JOIN d_collaborator_plan AS p
                            ON c.charac_plan_id = p.charac_plan_id )
               ON e.emp_id = p.emp_id
WHERE  ( ( c.date_cessibility ) <
c.date_cessibility < Nz(c.date_vesting_4, Nz(c.date_vesting_3,Nz(c.date_vesting_2,Nz(c.date_vesting_1,getdate()))))
       ) 

答案 1 :(得分:1)

它“崩溃”,因为语法与Access SQL不同。

所以在Access和查询中找到一个教程。你需要一种与当前不同的方法。

答案 2 :(得分:0)

最后这就是答案:

   SELECT 
   e.emp_id,
   e.emp_lname,
   e.emp_fname,
   e.effective_date AS eff_date
 FROM   r_employee AS e
   INNER JOIN ( d_plan_characteristics AS c
                INNER JOIN d_collaborator_plan AS p
                        ON c.charac_plan_id = p.charac_plan_id )
           ON e.emp_id = p.emp_id
 WHERE  ( ( c.date_cessibility ) < Nz(c.Date_vesting_4, Nz(c.Date_vesting_3, Nz(c.Date_vesting_2, Nz(c.Date_vesting_1, ""))))

   );