0

I'm trying to create the NEW_FIELD below. This should be set to 1 for the first time an new identifier occurs (ordering by date). If the number of days between two rows in the date field is greater than 90 then NEW_FIELD should increase by 1. Any tips on how to code for the NEW_FIELD?

NEW_FIELD   Identifier  Date    Prev Date   days_diff
1           1567    18/11/2005  NULL        NULL
2           1567    23/02/2008  18/11/2005  827
2           1567    29/02/2008  23/02/2008  6
2           1567    7/03/2008   29/02/2008  7
3           1567    27/01/2011  23/05/2008  979
3           1567    3/02/2011   27/01/2011  7
3           1567    10/02/2011  3/02/2011   7
1           845136  1/06/2012   NULL            NULL
1           845136  1/06/2012   1/06/2012   0
1           845136  1/06/2012   1/06/2012   0
1           845136  2/06/2012   1/06/2012   1
1           845136  3/06/2012   2/06/2012   1
2           845136  1/08/2013   3/06/2012   424
2           845136  2/08/2013   1/08/2013   1
2           845136  3/08/2013   2/08/2013   1
2           845136  4/08/2013   3/08/2013   1

1 Answer 1

2

TRY This...

IF((DATEDIFF(DW,'2013-11-12','2014-02-12'))>=90 )
    BEGIN
        UPDATE YOUR_TABLENAME SET NEW_FIELD=((select (max(NEW_FIELD)+1) from YOUR_TABLENAME ))
    END
ELSE
    UPDATE YOUR_TABLENAME SET NEW_FIELD=((select (max(NEW_FIELD)) from YOUR_TABLENAME ))
1
  • Thank you for your help. It doesn't seem that the value will reset to 1 for each new identifier though?? Dec 12, 2013 at 0:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.