How to create a case statement on max expiry date

  • How can i create a CASe statement which will search for a maximum expiry date and update the year fiield

    e.g

    Induction No Acccess Code Expiry Date

    a2345 561 01-01-2001

    a2345 561 01-01-2003

    b2345 561 01-01-2001

    b2345 561 01-01-2006

    b2345 563 01-01-2001

    b2345 563 01-01-2003

    The rules are

    select the latest expiry date for each different type of access code, then update the year by 3 for 2006 expiry dates and 6 years update for 2003 expiry dates

    I would hope to see results as

    a2345 561 01-01-2009

    b2345 561 01-01-2009

    b2345 563 01-01-2009

    Complicated i know but any help would be appreciated

  • This will work without a CASE and will change any year to 2009 if the MAX(expiry_date) is not 2003 or 2006.

    USE tempdb;

    GO

    DECLARE @test-2 TABLE(induction_no CHAR(5), access_code INT, expiry_date SMALLDATETIME)

    INSERT INTO @test-2 (

    induction_no,

    access_code,

    expiry_date

    )

    SELECT

    'a2345', 561 , '01-01-2001'

    UNION ALL

    SELECT

    'a2345', 561 , '01-01-2003'

    UNION ALL

    SELECT

    'b2345' , 561 , '01-01-2001'

    UNION ALL

    SELECT

    'b2345' , 561, '01-01-2006'

    UNION ALL

    SELECT

    'b2345' , 563, '01-01-2001'

    UNION ALL

    SELECT

    'b2345' , 563 , '01-01-2003'

    SELECT

    induction_no,

    access_code,

    DATEADD(YEAR, DATEDIFF(YEAR, MAX(expiry_date), '1/1/2009'), MAX(expiry_date))

    FROM

    @test-2

    GROUP BY

    induction_no,

    access_code

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply