Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Compare rows in the same table and group the data Expand / Collapse
Author
Message
Posted Tuesday, September 18, 2012 7:47 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 28, 2013 5:23 PM
Points: 44, Visits: 187
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.

Post #1361086
Posted Wednesday, September 19, 2012 1:46 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1361173
Posted Wednesday, September 19, 2012 5:33 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 28, 2013 5:23 PM
Points: 44, Visits: 187
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.
Post #1361696
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse