I have run into a complex situation. I searched all the blogs but could not find any answers close to my expectation. I am running the following query :
**Table1 Table2**
**ID Status RptGChng RptAChng RptRChng | ID Status**
8614 Green 0 0 0 | 8614 Red
8548 Unknown 0 0 0 | 8548 Amber
2591 Amber 0 0 0 | 2591 Amber
7813 Green 0 0 0 | 7813 Green
8413 Red 0 0 0 | 8413 Red
8183 Green 0 0 0 | 8183 Green
7431 Red 0 0 0 | 7431 Red
7399 Green 0 0 0 | 7399 Red
7776 Unknown 0 0 0 | 7776 Unknown
8609 Green 0 0 0 | 8609 Green
8068 Green 0 0 0 | 8068 Green
The RptGChng should give me a value of 1 if there has been a change in Green value of the id from Table1 to Table2. I need to populate in this manner for RptAChng -> Amber and RptRChng -> Red. The code that I wrote to arrive at this was as follows:
set [RptGChng] =
IIF(([Status]='Green'),1,0)-
IIF(([Status]=(
SELECT b.[Status] FROM [Table1] a INNER JOIN [Table2] b ON
a.[id]= b.[id] and b.[Status]='Green')),1,0)