My original table 'T1' looks like this:
ID Date Order_ind Var2 Var3
1 1/1/2015 1 ..... .....
1 1/5/2015 1 ..... .....
1 1/5/2015 2 ..... .....
2 1/10/2015 1 ..... .....
2 1/20/2015 1 ..... .....
2 1/20/2015 2 ..... .....
2 1/20/2015 3 ..... .....
The final table that I want to create is adding an additional variable 'new_var' based on some criteria. As you may notice, there are some records with the same date, and those criteria only work on the first record (order_ind=1). For the rest of the records with the same date, such as order_ind=2, or 3, the new_var value should be the same with the order_ind=1 record.
ID Date order_ind Var1 Var2 new_var
1 1/1/2015 1 ..... ..... 1
1 1/5/2015 1 ..... ..... 0
1 1/5/2015 2 ..... ..... 0
2 1/10/2015 1 ..... ..... 0
2 1/20/2015 1 ..... ..... 1
2 1/20/2015 2 ..... ..... 1
2 1/20/2015 3 ..... ..... 1
The SQL codes that I wrote are like these:
SELECT *,
CASE
WHEN order_ind=1 and (criteria1....) THEN '1'
WHEN order_ind=1 and (criteria2....) THEN '0'
WHEN order_ind<>1 .......(please advise how to code this)
END AS new_var
FROM T1
;
Any idea how to write the code for records with order_ind<>1?