lundi 13 juin 2016

Update Statement Logic

I am looking to change the current update statement logic that put all cards from hold (3) to active (1) with the following update statement:

UPDATE card
SET cardstatusid   = 1,
WHERE cardid      = vcardid
AND cardstatusid   = 3;

I need to change it to:

UPDATE card
SET cardstatusid   = 1, -- I need to change this 1 to some logic
WHERE cardid      = vcardid
AND cardstatusid   = 3;

Please read below the sample data:

select * from card; 

cardid cardstatusid personid

1234   4                  msmith
1255   4                  agrey

Lets say I change the status from '4' (closed) to '1' (preactive) for smith and '2' (active) for grey. It would give me the following:

select * from card; 

cardid cardstatusid personid

1234   1                  msmith
1255   2                  agrey

Now I run a job that changes the status to '3' for both of them. It would look like below:

select * from card; 

cardid cardstatusid personid

1234   3                  msmith
1255   3                  agrey

CardAudit table looks like this:

select * from cardaudit; 

cardauditid cardid cardstatusid personid

5677        1234   4            msmith
5678        1234   1            msmith
4389        1255   4            agrey
4390        1255   2            agrey

Now, I want to write an update statement where I can put the cards back to their previous status (which is max(cardauditid) for a particular user). So, I need to pick cardauditid 5678 and 4390.

Also, I cannot drop and create tables or anything else. Only, the current logic need to change or I can write a new procedure.

Thanks

Aucun commentaire:

Enregistrer un commentaire