• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Flag records based on service date

Please see attached excel file for sample data.
I want to mark records from the same customer as Index / ReIndex  (in the service_type column) based on the following rule:

Flag all records with svc_order =1 as 'Index' in Service_type column.

If customer_id is same
and svc_order>1
and DateDiff in days (svc_start_date for previous record where svc_order = svc_order-1, same customer_id) <=30

Then Flag it as Re-Index.

Attached sample data in excel file.
service_Type column will be initially null and updated as mentioned in the above logic.

How can I accomplish this using sql?
1 Solution
Brian CroweDatabase AdministratorCommented:
UPDATE myTable
SET service_type =
      WHEN svc_order = 1 THEN 'Index'
      WHEN DATEDIFF(DAY, myTablePrev.svc_start_date, myTable.svc_start_date) <= 30 THEN 'RE-Index'
FROM myTable
LEFT OUTER JOIN myTable AS myTablePrev
   ON myTable.customer_id = myTablePrev.customer_id
   AND myTable.svc_order = myTablePrev.svc_order + 1
patd1Author Commented:
Awesome. Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now