jeudi 21 janvier 2016

SQL Where exists case statement

I'm not sure how to combine a if/case with a where exists statement. All of the below fields live in t_phone

This is the set up - there is a phone temp variable that is declared

Declare @phone varchar(25)

`select @phone = ....`

What we need to say is for a given customer_no if phone_type (from t_phone) exists for that customer_no, with a type of 25 use the phone_no associated with type = 25, else use type 2.

so for example

phone_no          type           customer_no
1234567890          2                  4
0987654321          25                 4
6537327345          2                  8

based on the above example for customer 4 we want to set @phone = 0987654321, since there exists a type 25, but for customer number 8 we want to use type 2 since there is no alternative 25 type.

Aucun commentaire:

Enregistrer un commentaire