| Author |
Topic |
|
_polarix_
Starting Member
5 Posts |
Posted - 2008-05-08 : 06:41:07
|
I have a table, with rows like this:id --- from --- to6 5 316 31 306 30 296 29 287 56 597 59 60etc... I'd like to have a table with this result:id --- from --- to6 5 287 56 60etc... Thank you. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-05-08 : 06:57:49
|
| select id,min([from]) as [from], min([to]) as [to] from your_tablegroup by idMadhivananFailing to plan is Planning to fail |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-05-08 : 07:00:47
|
quote: Originally posted by madhivanan select id,min([from]) as [from], min([to]) as [to] from your_tablegroup by idMadhivananFailing to plan is Planning to fail
Won't that give...6 5 287 56 59?Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-05-08 : 07:04:36
|
Well, here's something. I suspect it isn't the full story, so please provide sample data which breaks it, and we can go from there...declare @t table (id int, vfrom int, vto int)insert @t select 6, 5, 31union all select 6, 31, 30union all select 6, 30, 29union all select 6, 29, 28union all select 7, 56, 59union all select 7, 59, 60; with a as (select id, vfrom from @t a where not exists (select * from @t where id = a.id and vto = a.vfrom)), b as (select id, vto from @t a where not exists (select * from @t where id = a.id and vfrom = a.vto)), c as (select id, vfrom, null as vto from a union all select id, null, vto from b)select id, max(vfrom) as vfrom, max(vto) as vto from c group by id/* Resultsid vfrom vto----------- ----------- -----------6 5 287 56 60*/ Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
_polarix_
Starting Member
5 Posts |
Posted - 2008-05-08 : 07:20:18
|
| [id] [from] [to]2 10 112 16 192 19 182 18 202 20 85 10 1128 12 1128 16 1229 12 1146 12 1146 22 1646 21 2247 12 1148 12 1158 28 2758 29 2858 30 2958 31 3058 5 3161 26 2161 27 2665 31 34result:[id] [from] [to]2 10 112 16 85 10 1128 16 1129 12 1146 12 1146 21 1647 12 1148 12 1158 5 2761 27 2165 31 34I don't know if it's possible !!! |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-05-08 : 07:49:56
|
Try this...declare @t table (id int, vfrom int, vto int)insert @t select 2, 10, 11union all select 2, 16, 19union all select 2, 19, 18union all select 2, 18, 20union all select 2, 20, 8union all select 5, 10, 11union all select 28, 12, 11union all select 28, 16, 12union all select 29, 12, 11union all select 46, 12, 11union all select 46, 22, 16union all select 46, 21, 22union all select 47, 12, 11union all select 48, 12, 11union all select 58, 28, 27union all select 58, 29, 28union all select 58, 30, 29union all select 58, 31, 30union all select 58, 5, 31union all select 61, 26, 21union all select 61, 27, 26union all select 65, 31, 34; with t1 (id, vInitial, vfrom, vto, Level) as ( select id, vfrom, vfrom, vto, 1 from @t a where not exists (select * from @t where id = a.id and vto = a.vfrom) union all select a.id, vInitial, b.vfrom, b.vto, Level + 1 from t1 a inner join @t b on a.id = b.id and a.vto = b.vfrom ), t2 as (select *, row_number() over (partition by id, vInitial order by Level desc) as RowNumber from t1)select id, vInitial as vfrom, vto from t2 where RowNumber = 1/* Resultsid vfrom vto----------- ----------- -----------2 10 112 16 85 10 1128 16 1129 12 1146 12 1146 21 1647 12 1148 12 1158 5 2761 27 2165 31 34*/ Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
_polarix_
Starting Member
5 Posts |
Posted - 2008-05-08 : 09:45:01
|
I'm very surprised !! You are a genius !!I'll try to understand the sintax Thank you very much._Polarix_ |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-05-08 : 10:07:13
|
Thanks The recursive cte is the tricky bit. There are lots of articles on it online...http://www.google.com/search?q=recursive+cteRyan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|