| Author |
Topic |
|
ganeshkumar08
Posting Yak Master
187 Posts |
Posted - 2010-03-29 : 07:25:49
|
| Hi,I have table structure as belowMatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss--------------------------------------------------------------------6824 0 592 0.400 1.300 0.9006824 0 592 1.300 3.600 2.3006824 0 1519 3.600 5.500 1.9006824 0 1519 5.500 9.200 3.7006824 0 847 9.200 11.600 2.4006824 0 658 11.600 13.200 1.6006824 0 592 13.600 13.900 1.0006824 0 592 13.900 14.000 2.3006824 0 1519 14.900 15.600 2.9006824 0 1519 15.600 17.600 3.700My output should be as shown belowMatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss--------------------------------------------------------------------6824 0 592 0.400 3.600 3.2006824 0 1519 3.600 9.200 5.6006824 0 847 9.200 11.600 2.4006824 0 658 11.600 13.200 1.6006824 0 592 13.600 14.000 2.3006824 0 1519 14.900 17.600 6.600--------------------------------------------------------------------Here we must do row by row operationfor example for below data6824 0 592 0.400 1.300 0.9006824 0 592 1.300 3.600 2.300continously Player 592 repeats 2 time, so for this player we have start time and end time.for the above data we need to show in single row6824 0 592 0.400 3.600 2.200How can i achieve this, the results should be shown in temporary table.ThanksGaneshSolutions are easy. Understanding the problem, now, that's the hard part |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-29 : 08:00:21
|
| What is the logic behind making end time 2.300 to 2.200Vaibhav T |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-29 : 08:20:06
|
I'd be interested in other approaches but here's my stab at the requirement.I've added an extra line of data so that there is at least one 3 line concatenation.DECLARE @matchDetails TABLE ( [MatchID] INT , [SegmentID] INT , [playerStart] INT , [timeStart] DECIMAL(9,3) , [timeEnd] DECIMAL(9,3) , [timeInPoss] DECIMAL(8,3) )INSERT @matchDetails SELECT 6824, 0, 592, 0.400, 1.300, 0.900UNION SELECT 6824, 0, 592, 1.300, 3.600, 2.300UNION SELECT 6824, 0, 592, 3.600, 10.000, 6.400UNION SELECT 6824, 0, 1519, 3.600, 5.500, 1.900UNION SELECT 6824, 0, 1519, 5.500, 9.200, 3.700UNION SELECT 6824, 0, 847, 9.200, 11.600, 2.400UNION SELECT 6824, 0, 658, 11.600, 13.200, 1.600UNION SELECT 6824, 0, 592, 13.600, 13.900, 1.000UNION SELECT 6824, 0, 592, 13.900, 14.000, 2.300UNION SELECT 6824, 0, 1519, 14.900, 15.600, 2.900UNION SELECT 6824, 0, 1519, 15.600, 17.600, 3.700; WITH combinedRows ( [MatchID] , [SegmentID] , [playerStart] , [timeStart] , [timeEnd] , [timeInPoss] , [level] , [rowID] )AS ( -- Anchor Definition (The rows with no preceeding row with same startend date -- As their start Date SELECT md.[matchID] , md.[segmentID] , md.[playerStart] , md.[timeStart] , md.[timeEnd] , CAST(md.[timeInPoss] AS DECIMAL(9,3)) , 0 AS [level] , NEWID() AS [rowID] FROM @matchDetails md WHERE NOT EXISTS ( SELECT 1 FROM @matchDetails md2 WHERE md2.[matchId] = md.[matchID] AND md2.[playerStart] = md.[playerStart] AND md2.[segmentID] = md.[segmentID] AND md2.[timeEnd] = md.[timeStart] ) -- Recusive Definition UNION ALL SELECT cr.[matchID] , cr.[segmentID] , cr.[playerStart] , cr.[timeStart] , md.[timeEnd] , CAST(cr.[timeInPoss] + md.[timeInPoss] AS DECIMAL(9,3)) , cr.[level] + 1 , cr.[rowId] FROM combinedRows cr JOIN @matchDetails md ON md.[matchID] = cr.[matchId] AND md.[playerStart] = cr.[playerStart] AND md.[segmentID] = cr.[segmentID] AND md.[timeStart] = cr.[timeEnd] )SELECT [matchId] , [segmentID] , [playerStart] , [timeStart] , [timeEnd] , [timeInPoss]FROM ( SELECT [matchId] , [segmentID] , [playerStart] , [timeStart] , [timeEnd] , [timeInPoss] , ROW_NUMBER() OVER( PARTITION BY [rowId] ORDER BY [level] DESC ) AS [rowPos] FROM combinedRows ) crWHERE cr.[rowPos] = 1ORDER BY cr.[matchId] , cr.[playerStart] Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-03-29 : 08:26:16
|
You need to group by PlayerStart and the difference in the order overall, based on TimeStart, and the order within PlayerStart.(This technique won a competition.)Something like:;WITH OrdersAS( SELECT MatchID, SegmentID, PlayerStart, TimeStart, TimeEnd, TimeinPoss ,ROW_NUMBER() OVER (PARTITION BY MatchID, SegmentID ORDER BY TimeStart) - ROW_NUMBER() OVER (PARTITION BY MatchID, SegmentID, PlayerStart ORDER BY TimeStart) AS OrderDiff FROM YourTable), OrdersGroupAS( SELECT MatchID, SegmentID, PlayerStart, OrderDiff ,MIN(TimeStart) AS TimeStart ,MAX(TimeEnd) AS TimeEnd ,SUM(TimeinPoss) AS TimeinPoss FROM Orders GROUP BY MatchID, SegmentID, PlayerStart, OrderDiff)SELECT MatchID, SegmentID, PlayerStart, TimeStart, TimeEnd, TimeinPossFROM OrdersGroupORDER BY TimeStart [EDIT]: Just looked at this again.While it should work with the test data supplied it will failif another play starts while the previous one is running.The OP will need to clarify the requirement. |
 |
|
|
ganeshkumar08
Posting Yak Master
187 Posts |
Posted - 2010-03-29 : 09:54:56
|
| Thank you Ifor, i am very happy with your resultsSolutions are easy. Understanding the problem, now, that's the hard part |
 |
|
|
ganeshkumar08
Posting Yak Master
187 Posts |
Posted - 2010-03-30 : 05:43:25
|
| Hi Ifor,I need a small change in the ;WITH OrdersAS( SELECT MatchID, SegmentID, PlayerStart, TimeStart, TimeEnd, TimeinPoss ,ROW_NUMBER() OVER (PARTITION BY MatchID, SegmentID ORDER BY TimeStart) - ROW_NUMBER() OVER (PARTITION BY MatchID, SegmentID, PlayerStart ORDER BY TimeStart) AS OrderDiff FROM YourTable), OrdersGroupAS( SELECT MatchID, SegmentID, PlayerStart, OrderDiff ,MIN(TimeStart) AS TimeStart ,MAX(TimeEnd) AS TimeEnd ,SUM(TimeinPoss) AS TimeinPoss FROM Orders GROUP BY MatchID, SegmentID, PlayerStart, OrderDiff)SELECT MatchID, SegmentID, PlayerStart, TimeStart, TimeEnd, TimeinPossFROM OrdersGroupORDER BY TimeStartMy data will be as belowMatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss6824 0 592 0.400 1.300 0.9006824 0 592 1.300 3.600 2.3006824 0 592 3.900 4.000 2.3006824 0 1519 4.000 5.500 1.9006824 0 1519 5.500 9.200 3.7006824 0 847 9.200 11.600 2.400My Out put should be as belowMatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss6824 0 592 0.400 3.600 3.2006824 0 592 3.900 4.000 2.3006824 0 1519 4.000 9.200 5.6006824 0 847 9.200 11.600 2.400another condition should be included, i.e, TimeEnd should be same as Timestart then add both this as timeinPossThanksGaneshSolutions are easy. Understanding the problem, now, that's the hard part |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-03-30 : 09:23:53
|
| You had better go for something like Transact Charlie's solution. |
 |
|
|
|
|
|