on 2012 Apr 18 5:17 PM
I've worked with Microosft SQL for years and am writing my first SQL Anywhere application. There is a simple query I'm trying to write that I can't get to work without syntax issues. Can someone please show me the correct syntax? I can't seem to get a named subquery to work
Select * from (select field1 from table1) as MyQuery join table1 on table1.field2 = MyQuery.field1
Thank you for everyone's help. It appears that the version I was working against wanted me to name the columns using a syntax I was unfamiliar with.
My solution:
Select * from (select field1 from table1) as MyQuery (MyNamedField) join table1 on table1.field2 = MyQuery.MyNamedField
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Link that allowed me to find the answer.
What exactly are you trying to get as a result set? It looks like you want the rows from table1 that have a field1 value equal to any of the values in the field2 column of the same table?
If the above is correct, then try this:
select * from table1 where field1 in ( select distinct field2 from table1 );
HTH
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I didn't post the full query in my question, because I was concerned the complexity would throw people off when the syntax issue I was having problem with is straightforward. The problem I was trying to solve involves a table where one field may be duplicated on many records. If the field is duplicated, I only want to return one record. The record returned should have the maximum value in a different field within it's group. If that's unclear, here is a more relavent example of the query I was trying to get.
Select * from (Select Max(UniqueField), NonUniqueField from Table1 group by NonUniqueField) as UniqueSubset (MaxUniqueField, NonUniqueField) left join Table1 on UniqueSubset.MaxUniqueField = Table1.UniqueField and UniqueSubset.NonUniqueField = Table1.NonUniqueField
The "in" command doesn't really accomplish this well because it's a composite key I'm after (two fields).
That exact syntax should work - what version are you using (run select @@version) and what error message do you receive?
For example, here is a sample query that uses a similar query against the system catalog, and it does work with v 12.0.1.3554:
select * from (select table_id, table_name from systable) as ST join syscolumn on syscolumn.table_id = ST.table_id where table_name = 'dummy'
Note that the "as" before the derived query's alias (here "ST") is optional, however the alias itself is necessary.
(FWIW, I would usually use "inner join" instead of just "join" - in order to distinguish that from outer joins, and would order the join condition in the same order as the according tables/derived queries, i.e. I would swap the on condition...)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
67 | |
10 | |
10 | |
10 | |
10 | |
8 | |
8 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.