Compare rows in the same table and group the data

  • Hello All,

    Please point me in the right direction as to where i can start to resolve this problem.

    I have a table , below is the dummy data and the desired result.

    I need to group the data based on the ProductID, Date,LinesTotal, LinesOutOfService.

    I need to check each row in the table to see for that particular product has the different range of values for totallines and Linesoutofservice when compared to the rows below if yes then insert that date where a different range is encountered into the "End Date" column. If no change in the values are detected then insert a future date something like 01/12/2050.

    Data

    ProductID Date TotalLines LinesOutOfService

    522 5/04/2010 345 5

    522 6/04/2010 345 5

    522 7/04/2010 120 4

    522 8/04/2010 345 5

    522 9/04/2010 345 5

    522 10/04/2010 345 5

    522 11/04/2010 117 20

    522 12/04/2010 345 5

    522 14/04/2010 345 5

    522 15/04/2010 260 10

    522 16/04/2010 345 5

    522 17/04/2010 345 5

    522 19/04/2010 345 5

    Result

    ProductID Start Date End Date TotalLines LinesOutOfService

    522 5/04/2010 6/04/2010 345 5

    522 7/04/2010 7/04/2010 120 4

    522 8/04/2010 10/04/2010 345 5

    522 11/04/2010 11/04/2010 117 20

    522 12/04/2010 14/04/2010 345 5

    522 15/04/2010 15/04/2010 260 10

    522 16/04/2010 01/12/2050 345 5

    Thanks in advance for your time and suggestions.

  • You might want to take a look at this article by Jeff Moden to get you started: http://www.sqlservercentral.com/articles/T-SQL/71550/, although that endpoint requirement is a bit odd and may take some workarounds.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Hi dwain,

    The link help me to reach a solution.

    I have used the SAP Data services History Transform and acheived the desired result.

    Thanks, Shilpa.

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

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