lundi 27 février 2017

IF ELSE in sql query

I would like to know what's wrong in this query. In fact I have 3 tables and the idea is that I have 4 dates date_vesting_1 to date_vesting_4, and I should check the Last completed date to compere it with another date. So I used If ELSE in the query to start from date 4 to date 1 but it crashes every time I excute.

Thank you in advance.

   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) );

Aucun commentaire:

Enregistrer un commentaire