Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Doubt in Query Results showing

Author  Topic 

ganeshkumar08
Posting Yak Master

187 Posts

Posted - 2010-03-29 : 07:25:49
Hi,
I have table structure as below

MatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss
--------------------------------------------------------------------
6824 0 592 0.400 1.300 0.900
6824 0 592 1.300 3.600 2.300
6824 0 1519 3.600 5.500 1.900
6824 0 1519 5.500 9.200 3.700
6824 0 847 9.200 11.600 2.400
6824 0 658 11.600 13.200 1.600
6824 0 592 13.600 13.900 1.000
6824 0 592 13.900 14.000 2.300
6824 0 1519 14.900 15.600 2.900
6824 0 1519 15.600 17.600 3.700

My output should be as shown below

MatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss
--------------------------------------------------------------------
6824 0 592 0.400 3.600 3.200
6824 0 1519 3.600 9.200 5.600
6824 0 847 9.200 11.600 2.400
6824 0 658 11.600 13.200 1.600
6824 0 592 13.600 14.000 2.300
6824 0 1519 14.900 17.600 6.600
--------------------------------------------------------------------

Here we must do row by row operation
for example for below data
6824 0 592 0.400 1.300 0.900
6824 0 592 1.300 3.600 2.300
continously 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 row
6824 0 592 0.400 3.600 2.200


How can i achieve this, the results should be shown in temporary table.

Thanks
Ganesh



Solutions 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.200

Vaibhav T
Go to Top of Page

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.900
UNION SELECT 6824, 0, 592, 1.300, 3.600, 2.300
UNION SELECT 6824, 0, 592, 3.600, 10.000, 6.400
UNION SELECT 6824, 0, 1519, 3.600, 5.500, 1.900
UNION SELECT 6824, 0, 1519, 5.500, 9.200, 3.700
UNION SELECT 6824, 0, 847, 9.200, 11.600, 2.400
UNION SELECT 6824, 0, 658, 11.600, 13.200, 1.600
UNION SELECT 6824, 0, 592, 13.600, 13.900, 1.000
UNION SELECT 6824, 0, 592, 13.900, 14.000, 2.300
UNION SELECT 6824, 0, 1519, 14.900, 15.600, 2.900
UNION 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
)
cr
WHERE
cr.[rowPos] = 1
ORDER BY
cr.[matchId]
, cr.[playerStart]



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 over
all, based on TimeStart, and the order within PlayerStart.
(This technique won a competition.)
Something like:

;WITH Orders
AS
(
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
)
, OrdersGroup
AS
(
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, TimeinPoss
FROM OrdersGroup
ORDER BY TimeStart


[EDIT]: Just looked at this again.
While it should work with the test data supplied it will fail
if another play starts while the previous one is running.
The OP will need to clarify the requirement.
Go to Top of Page

ganeshkumar08
Posting Yak Master

187 Posts

Posted - 2010-03-29 : 09:54:56
Thank you Ifor, i am very happy with your results

Solutions are easy. Understanding the problem, now, that's the hard part
Go to Top of Page

ganeshkumar08
Posting Yak Master

187 Posts

Posted - 2010-03-30 : 05:43:25
Hi Ifor,

I need a small change in the ;WITH Orders
AS
(
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
)
, OrdersGroup
AS
(
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, TimeinPoss
FROM OrdersGroup
ORDER BY TimeStart


My data will be as below

MatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss
6824 0 592 0.400 1.300 0.900
6824 0 592 1.300 3.600 2.300
6824 0 592 3.900 4.000 2.300
6824 0 1519 4.000 5.500 1.900
6824 0 1519 5.500 9.200 3.700
6824 0 847 9.200 11.600 2.400

My Out put should be as below

MatchID SegmentID PlayerStart TimeStart TimeEnd TimeinPoss
6824 0 592 0.400 3.600 3.200
6824 0 592 3.900 4.000 2.300
6824 0 1519 4.000 9.200 5.600
6824 0 847 9.200 11.600 2.400

another condition should be included, i.e, TimeEnd should be same as Timestart then add both this as timeinPoss

Thanks
Ganesh

Solutions are easy. Understanding the problem, now, that's the hard part
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-03-30 : 09:23:53
You had better go for something like Transact Charlie's solution.
Go to Top of Page
   

- Advertisement -