Hi I am a bit stuck here. I am trying to create 3 labels for a contract called “Green”,”Yellow”, and “Red”. The labels are identified by the formula contractdate-curdate().
– Green if it is more than 90 days away.
– Yellow – if the = end date is between 31 and 89 days
– Red if the end date is 30 days or less
I need a way to either create a column for Label and put each contract in their respective color, or create 3 columns for each color with an identifier?
These are a couple queries i’ve made but with no luck.
select
contractname, contractenddate
from
contract
where
contractenddate between CurDate() and Date_Add(CurDate(), INTERVAL 30 DAY)
and contractenddate between CurDate() and Date_Add(CurDate(), INTERVAL 60 DAY)
and contractenddate between CurDate() and Date_Add(CurDate(), INTERVAL 90 DAY)
and
select contractname, date_sub(contractenddate,interval 0-30 day)as Red,
date_sub(contractenddate,interval 31-89 day)as Yellow,
date_sub(contractenddate,interval 90 day)as Green
from contract
I know these are incorrect but perhaps someone could help build on these? It would be GREATLY appreicated!!! One last note is I am unable to alter the database structure, I can only use select statements.
Thanks a TON in advance!
Something like this work? Look into using
DATEDIFFandCASE:Here is the SQL Fiddle.
And if you want the number of days away, you could add the following to your SQL Statement:
Alternatively, if you need Red, Yellow and Green as Columns, something very similar would work:
I’ve put an ‘X’ to mark which have which. Wasn’t quite clear what you were looking for.
More fiddle.
Good luck.