4

TABLE_A:

ID      TYPE    DATE_UPLOADED   EXPIRED
9872    APPLE   03-JAN-11       0
9874    MANGO   03-JAN-11       0
9873    GRAPE   03-JAN-11       0

TABLE_B:

TYPE    LIFE
APPLE   3
MANGO   2
GRAPE   1

What I would like to achieve here is to update the EXPIRED
field of TABLE_A to a value of 1 when it's DATE_UPLOADED
has exceeded it's LIFE for that type as of the current date.

This update query is where I'm currently stuck with. I know its
wrong, that's where I need your help.

Update query:

UPDATE TABLE_A
SET EXPIRED = 1
WHERE EXPIRED = 0
AND (TRUNC(SYSDATE) - TRUNC(DATE_UPLOADED)) >
(
    SELECT LIFE
    FROM TABLE_B 
);

Do note that the TYPE field may be anything and may be more
than what is given in the sample data.

John Doyle
  • 7,035
  • 5
  • 32
  • 39
cr8ivecodesmith
  • 1,921
  • 5
  • 19
  • 30

1 Answers1

4

You are not linking the two tables. Try something like this:

UPDATE TABLE_A
SET EXPIRED = 1
WHERE EXPIRED = 0
AND (TRUNC(SYSDATE) - TRUNC(DATE_UPLOADED)) >
(
    SELECT LIFE
    FROM TABLE_B 
    WHERE TYPE = TABLE_A.TYPE
);
David M
  • 69,925
  • 13
  • 153
  • 183