Add/update column based on multiple conditions in sql server 2008
I have a table and I need to add/update a column 'CD' with values 0 or 1
based on some conditions. My table is populated by work contracts, and a
same person (identified by ID_p) can have more than one contract. The
conditions are the following :
For each distinct ID_p there can only be "one row" with CD = 1
First, if C1 = 0 then [CD = 0]
If there is more than one distinct ID_p with C1 = 1, I use columns C2, C3
and C4 to determine which CD = 1. It is similar to an Olympic medal
system, with C2, C3 and C4 being gold, silver and bronze, respectively.
The following table is an example for the result I want for CD, based on
the other columns.
ID_p C1 C2 C3 C4 CD
1 1 3 3 3 1
2 0 3 3 3 0
3 1 3 3 3 1
3 0 3 3 3 0
4 1 2 3 3 0
4 1 3 2 2 1
5 1 1 3 3 0
5 1 2 3 3 0
5 1 3 0 0 1
No comments:
Post a Comment