lundi 13 juin 2016

Update Statement for Last Card Status

I have a card with cardstatus: 1 (open)

I got another card with cardstatus: 2 (closed)

I run a job that makes both of the cards on cardstatus: 3 (Hold)

Now, I want to write an update statement where it would set both of the cards to their previous statuses. I just gave you 1 and 1 cards as examples.

In reality there are 1000s of cards and obviously I don't want to write that many update statements and I don't want to change the current code that much. Currently, the logic in update statement puts all hold cards (cardstatus 3) to active (cardstatus 1).

card table has cardstatus (current status)

cardaudit table has cardstatus (prevous card statuses)

max(cardauditid) in cardaudit table would always be the laststatus --- this is what I want instead of all cards to active status

card and cardaudit tables have cardstatus in common

I am trying to write:

update card set cardstatus = (some query to get the previous status)

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.

Thanks

Aucun commentaire:

Enregistrer un commentaire