Home Forums SQL Server 2014 Development - SQL Server 2014 TSQL dense rank type question - finding the first date of a budget change (budget values can bounce back and forth in values) RE: TSQL dense rank type question - finding the first date of a budget change (budget values can bounce back and forth in values)

  • This will get you what you're after:

    SELECT

    cust_id

    , budget

    , LastYearBudget

    FROM

    (

    SELECT

    cust_id

    , budget

    , LAG(budget, 1, -1) OVER (PARTITION BY cust_id ORDER BY sDate) AS LastYearBudget

    FROM

    testTable

    ) tmp

    WHERE

    budget <> LastYearBudget

    AND

    LastYearBudget <> -1

    ;