on 2012 Jun 14 11:11 AM
Hi,
I have 2 tables,
the first has columns SourceCodeCity, TargetCodeCity and Distance (there is no NameCity on it).
The second has columns CodeCity and NameCity.
I need a query to show a table almost like this..
NameCity(SourceCodeCity) | NameCity(TargetCodeCity) | Distance
Can you help-me ?
I tried with select and some joins but does not work
Request clarification before answering.
Let's suppose the first table is called DISTANCES, the second table is called CITYNAMES.
You want something like
SELECT SOURCE.NameCity, TARGET.NameCity, DISTANCES.Distance FROM DISTANCES, CITYNAMES AS SOURCE, CITYNAMES AS TARGET WHERE DISTANCES.SourceCodeCity = SOURCE.CodeCity AND DISTANCES.TargetCodeCity = TARGET.CodeCity
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
'Tis an old and valuable technique, using different correlation names to allow a table to be joined to itself in a FROM clause.
So old, in fact, that it got dropped from the docs somewhere between Version 5.5 and 12... well, not dropped, but moved to an obscure topic that implies you must use the WITH clause (you don't): http://dcx.sybase.com/index.html#1201/en/dbusage/ug-commontblexpr-s-3099894.html
Perhaps this sentence from the Version 5.5 docs should be added to the V12 topic on the FROM clause http://dcx.sybase.com/index.html#1201/en/dbreference/from-statement.html
SQL Anywhere User's Guide
PART 6. SQL Anywhere Reference
CHAPTER 43. Watcom-SQL Statements
FROM clause
The correlation name is also necessary to distinguish between table instances when referencing the same table more than once in the same query.
@Glenn: Wouldn't you prefer a JOIN condition instead of a WHERE clause here, such as
SELECT SOURCE.NameCity, TARGET.NameCity, DISTANCES.Distance FROM DISTANCES INNER JOIN CITYNAMES AS SOURCE ON DISTANCES.SourceCodeCity = SOURCE.CodeCity INNER JOIN CITYNAMES AS TARGET ON DISTANCES.TargetCodeCity = TARGET.CodeCity
(Yes, the outcame is surely identical here.)
User | Count |
---|---|
61 | |
8 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.