Comparing values between tables and if same then use a different field?

I have 3 database tables:

  • customer_db
  • Table A
  • Table B

TableA is being used to run audience criteria and then data from table A is being pushed to Table B for archive. TableB schema is same as TableA, except TableB includes a timestamp column to keep a track of when row was added.

To populate Table A, I am running query like this (excluding customers who came in last 90 days):

Select cust_id, cust_email, First_Priority
from customer_db where First_Priority = A
and cust_id NOT IN (SELECT DISTINCT cust_id FROM TableB WHERE timestamp >= dateadd(d,-90,getdate()))

Now, I want to check, if same customer comes in AFTER 90 days and if First_Priority values is same as what First_Priority value was 90 days back (by checking TableB), if value is same then populate / check Second_Priority column value for customer.

Something like this: If customer has First_Priority = A and had TableB.First_Priority = A for the same customer where timestamp is after 90 days then use Second_Priority = A

I hope this example makes sense. I need some guidance on how to compare values in sql and is value is same when use a different column? any help would be appreciated.

Dynamic SQL.

please google search

Might be of help