Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a database with many tables, especially two tables one store paths and the other one store cities of a path :

Table Paths [ PathID, Name ]
Table Routes [ ID, PathID(Forein Key), City, GoTime, BackTime, GoDistance, BackDistance]

Table Paths :

PathID.......Name
1...................NewYork Casablanca Alpha 1
7...................Paris Tokyo 6007 10:00

Table Routes :

ID..........PathID..........City....................GoTime..........BackTime..........GoDistance..........BackDistance
1............1....................NewYork...........08:00.................23:46...............5810....................NULL
2............1....................Casablanca......15:43..................16:03...............NULL....................5800
3............2....................Paris...................10:20..................14:01...............3215....................NULL
4............2....................Cairo..................14:50..................09:31...............2425....................3215
3............2....................Dubai.................18:21..................06:00...............NULL....................2425

I want a Query that gives me all the possible combinations inside the same Path, something like :

PathID..........CityFrom..........CityTo..........Distance

I don't know if I made myself clear or not but hope you guys could help me, thanx in advance.
Posted
Updated 20-Jun-13 2:02am
v2
Comments
Rajesh Anuhya 20-Jun-13 8:05am    
Have you Tryed anything? --RA
Xachigami Alpha 20-Jun-13 9:29am    
I already done 2 queries that has one parameter PathID then it calculates portion by portion for normal path (go) and inverse path (back).
Suppose that I have the PathID = 2
the first (Go) query returns :

Paris Cairo 3215
Cairo Dubai 2425

the second (back) query returns :

Dubai Cairo 2425
Cairo Paris 3215

P.S: in my queries I am counting on the Table Routes ID to get the job done (because I assume that the end user will write the cities in their native ranking like I did in my example)
PratikThummar 25-Feb-18 3:11am    
What about time....

1 solution

SQL
SELECT
  r1.PathId,
  Go_Back = CASE WHEN GoDistance IS NULL THEN 'Back' ELSE 'Go' END,
  CityFrom = r1.City,
  CityTo = r1.City,
  Distance = ISNULL(GoDistance, BackDistance)
FROM
  dbo.Routes r1
    INNER JOIN
  dbo.Routes r2
      ON
        r1.PathID = r2.PathID AND
        r1.City <> r2.City
 
Share this answer
 
Comments
Xachigami Alpha 20-Jun-13 11:01am    
thank you for your contribution but I am afraid that your query don't resolve my problem ( but it did gives me a new way ; )

thanks a lot.
Xachigami Alpha 20-Jun-13 11:43am    
You query didn't worked at first time here the version I use ( if you give your permission of course)
<pre lang="SQL">SELECT
r1.PathID ,
Go_Back = CASE WHEN r1.GoDistance IS NULL THEN 'Back' ELSE 'Go' END,
CityFrom = r1.City,
CityTo = r2.City,
Distance = ISNULL(r1.GoDistance, r1.BackDistance )
FROM
dbo.Routes r1
INNER JOIN
dbo.Routes r2
ON
r1.PathID = r2.PathID AND
r1.City <> r2.City</pre>

The result (Of the query):


___________________________________________________________________________________
|PathID---------|Go-Back--------|CityFrom-------|CityTo --------|Distance-------|
___________________________________________________________________________________
|1--------------|Go-------------|New York--------|Casablanca----|5810-----------|
|1--------------|Back-----------|Casablanca-------|New York-----|5800-----------|
|7--------------|Go-------------|Paris-------------|Cairo----------|3215-----------|
|7--------------|Go-------------|Paris-------------|Dubai---------|3215-----------|
|7--------------|Go-------------|Cairo------------|Paris-----------|2425-----------|
|7--------------|Go-------------|Cairo------------|Dubai----------|2425-----------|
|7--------------|Back-----------|Dubai------------|Paris-----------|2425-----------|
|7--------------|Back-----------|Dubai------------|Cairo----------|2425-----------|
____________________________________________________________________________________

The correct answer :



<blockquote class="FQ">___________________________________________________________________________________
|PathID---------|Go-Back--------|CityFrom-------|CityTo --------|Distance-------|
___________________________________________________________________________________
|1--------------|Go-------------|New York--------|Casablanca----|5810-----------|
|1--------------|Back-----------|Casablanca-------|New York-----|5800-----------|
|7--------------|Go-------------|Paris-------------|Cairo----------|3215-----------|
|7--------------|Go-------------|Paris-------------|Dubai---------|5640-----------|
|7--------------|Go-------------|Cairo------------|Paris-----------|2425-----------|
|7--------------|Back-----------|Cairo------------|Dubai----------|2425-----------|
|7--------------|Back-----------|Dubai------------|Paris-----------|5640-----------|
|7--------------|Back-----------|Dubai------------|Cairo----------|2425-----------|
____________________________________________________________________________________</blockquote>


<pre lang="xml">In fact your query does all the combinations but it doesn't calculate the distance</pre>

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