|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2004-02-09 : 06:51:29
|
.... or to use several "union" operators:create table flights (cost int, p1 char(1), p2 char(1))insert into flightsselect 200, 'a', 'b' union allselect 200, 'b', 'a' union allselect 300, 'b', 'c' union allselect 300, 'c', 'b' union allselect 500, 'c', 'd' union allselect 500, 'd', 'c' union allselect 777, 'a', 'd' union allselect 777, 'd', 'a' union allselect 140, 'a', 'r' union allselect 140, 'r', 'a' union allselect 220, 'r', 'd' union allselect 220, 'd', 'r' union allselect 800, 'x', 'y' union allselect 800, 'y', 'x'declare @p1 char(1), @p2 char(1)set @p1='a' set @p2='d'select cost, p1, p2, null, null, null, nullfrom flights where p1=@p1 and p2=@p2union allselect f1.cost+f2.cost cost, f1.p1, f1.p2, f2.p1, f2.p2, null, nullfrom flights f1 inner join flights f2 on f1.p2=f2.p1where f1.p1=@p1 and f2.p2=@p2union allselect f1.cost+f2.cost+f3.cost, f1.p1, f1.p2, f2.p1, f2.p2, f3.p1, f3.p2from flights f1 inner join flights f2 on f1.p2=f2.p1 inner join flights f3 on f2.p2=f3.p1where f1.p1=@p1 and f3.p2=@p2 and f1.p2<>@p2 and f2.p2<>@p1drop table flightscost p1 p2 ----------- ---- ---- ---- ---- ---- ---- 777 a d NULL NULL NULL NULL360 a r r d NULL NULL1000 a b b c c d |
 |
|