Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table Table1 as follows
ID	Action 	Value
1	BEGIN	1
1	END	5
2	BEGIN	20
2	END	25
1	BEGIN	10
1	END	15


Please help me to write a TSQL query which create a result set as follows.
ID	BEGIN	END
1	1	5
2	20	25
1	10	15
Posted
Updated 22-Jan-12 23:32pm
v2
Comments
[no name] 23-Jan-12 8:38am    
Thank you all for your support, i am submitting two approach to resolve my scenario

Use PIVOT - UNPIVOT

Using PIVOT and UNPIVOT[^]

Pivoting data in SQL Server[^](Alternate ways)
 
Share this answer
 
Comments
[no name] 23-Jan-12 6:31am    
Thanks Tharaja,

I am facing challenges in using PIVOT, please see the 'Solution2', the aggregate used in PIVOT is not retrieving expected data.
[no name] 23-Jan-12 6:34am    
my expected Result
ID BEGIN END
1 1 5
2 20 25
1 10 15

Pivot query is retrieving (because Aggregate SUM is used in PIVOT)
ID BEGIN END
1 11 20
2 20 25
Amir Mahfoozi 26-Jan-12 5:20am    
+5
Solution: 1

SQL
select * from (
select ID,[Action],value from @TableData) s_tab pivot (
max(value)  for [Action] in ([BEGIN],[END])) as pvt

union

select * from (
select ID,[Action],value from @TableData) s_tab pivot (
min(value)for [Action] in ([BEGIN], [END])) as pvt


Solution 2
SQL
--Select Query
SELECT Beginning.ID AS [ID]
         ,Beginning.Value AS [Begin]
         ,Ending.Value AS [End]
FROM @Table1 AS Beginning
      OUTER APPLY (
                        SELECT TOP 1 Value
                        FROM @Table1 AS ext
                        WHERE ext.[Action] = 'END'
                                AND ext.ID = Beginning.ID
                                AND ext.Value > Beginning.Value
                        ORDER BY ext.Value ASC
                   ) AS Ending
WHERE [Action] = 'BEGIN'
 
Share this answer
 
v2
Comments
Heino Zunzer 23-Jan-12 11:08am    
You do realise that both above queries only work with the specific data you have entered?
Try changing the data to
id action value
1 BEGIN 9
1 END 16
2 BEGIN 20
2 END 25
1 BEGIN 10
1 END 15
and you'll see that you still have no solution, since the main problem is the same ID for lines 1, 2, 5 and 6.
This should be accomplish using PIVOT

read this

http://msdn.microsoft.com/en-us/library/ms177410.aspx[^]
 
Share this answer
 
Comments
[no name] 23-Jan-12 6:31am    
Thanks Rahul,
I am facing challenges in using PIVOT, please see the 'Solution2', the aggregate used in PIVOT is not retrieving expected data.
hi try with this,


SQL
SELECT id, [BEGIN] , [END]
FROM
(SELECT id, action, value
FROM temp) ps
PIVOT
(
SUM (value)
FOR [action] IN
( [BEGIN], [END])
) AS pvt
group by  id,[BEGIN],[END]
 
Share this answer
 
Comments
[no name] 23-Jan-12 6:29am    
Thanks,

This query will not work retrieve the correct result, its taking sum of all values in ID '1' and ID '2'.
[no name] 23-Jan-12 6:32am    
my expected Result
ID BEGIN END
1 1 5
2 20 25
1 10 15

Pivot query is retrieving (because Aggregate SUM is used in PIVOT)
ID BEGIN END
1 11 20
2 20 25
palraj001 23-Jan-12 6:56am    
if you want to make this out put , you have to add another column for grouping ID
palraj001 23-Jan-12 7:08am    
1 BEGIN 1 1
1 BEGIN 10 2
1 END 5 1
1 END 15 2
2 BEGIN 20 3
2 END 25 3
[no name] 23-Jan-12 8:42am    
Another solution is submitted in 'Solution5', please review
I think it will never work, since line 1 and 2 and line 5 and 6 have the same ID. There is no way of knowing for the SQL Server, which lines belong together.

Assuming you have ID 3 in line 5 and 6 you can use this query:
ID	Action 	Value
1	BEGIN	1
1	END	5
2	BEGIN	20
2	END	25
3	BEGIN	10
3	END	15


SQL
select
    b.id, b.value as [BEGIN], e.value as [END]
from
    t1 b
    inner join t1 e on b.id = e.id
where
    b.action = 'BEGIN'
    and e.action = 'END'


Result:
id	BEGIN	END
1	1	5
2	20	25
3	10	15
 
Share this answer
 
v3
Comments
[no name] 23-Jan-12 8:39am    
thank you,
Please find Solution 5 for a few approaches to handle this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900