How can I select more than one table in sql server while using a single where clause condition
for both tables? For example, if I had two tables named table1, table2, then:
select * from table1,table2 where rollno=1
But in the above query it gives me error -
ambiguous column
which is logical.
Give Alias name or use the tablename to avoid ambiguous column error and try.
select * from table1 A,table2 B where A.rollno=1 AND B.rollno=1
Related
I'm trying to optimize a SQL query that uses a GROUP BY across multiple tables. Essentially, I have multiple tables which all contain a PID column, and the output I need is a record of every PID in all of the tables as well as a count of how many records across all of those tables contain that PID. When trying to use GROUP BY PID, I get a "column ambiguously defined" error if using multiple tables. Here is an example of the code I am using to retrieve the proper data from one table (can ignore the where clause):
select pid, count(*)
from table1
where vendor_id in(1,2)
and delay_code <=23
and age between 18 and 49 and sex = 'M'
group by pid
Essentially, I want to do this across a group of tables (i.e. table1, table2, table3 etc), but can't figure out how to do so without getting a "column ambiguously defined" error.
You need to identify what record you are referencing. You can do that either by specifying the table, or using an alias. Aliases are required when you have multiple references to the same table.
Specify table:
SELECT table1.pid, COUNT(*)
FROM table1
GROUP BY table1.pid
Use alias:
SELECT t1.pid, COUNT(*)
FROM table1 AS t1
GROUP BY t1.pid
I'm trying to compare two tables in a DB2 database in z/OS using SPUFI to submit SQL queries.
I'm doing this by using EXCEPT to see the difference between two SELECT queries.
I need to filter the SELECT statement from the first query with a WHERE clause.
SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1'
EXCEPT
SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2
I got results back, but it also returned an error -199 Is this because the WHERE clause is not present in the second SELECT statement?
ERROR: ILLEGAL USE OF KEYWORD EXCEPT.
TOKEN <ERR_STMT> <WNG_STMT> GET SQL
SAVEPOINT HOLD FREE ASSOCIATE WAS EXPECTED
Try introducing parentheses e.g.
( SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1' )
EXCEPT
( SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2 )
I'm using t-sql,
I have two separate select statements. The first one involves nested queries based on parameters passed to the nested query.
Can i use the results of the first select statement as a table to use the "EXCEPT" operator or JOIN operator to connect with the select result set coming from the 2nd select statement in the format:
select col1,col2,col3 from tableOne where col3=(nested queries) as table1
<EXCEPT/JOIN>
select col3,col4,col5 from tableOne where col2=(nested queries) as table2
"as table1/2" gives me errors and "EXCEPT" key word, only excepts one column to do the comparison Are there better approaches for this?
how do i filter a column col1 with multiple values
select * from table where col1=2 and col1=4 and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'
i tired
select * from table where col1=2 or col1=4 and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'
the result of both queries is incorrect
the first one gives zero results
second one gives 3 results
the correct is 2 results
the sql will run against the sqlite db.
AND is evaluated before OR, so your query is equivalent to:
select *
from table
where col1=2 or (col1=4 and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1')
You need to explicitly group the conditions when mixing AND and OR:
select *
from table
where (col1=2 or col1=4) and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'
I'm struggling with the following problem. I want to restrict access to a table using Oracle VPD. In the where clause that the select policy function returns I need a reference to the table in a subquery as follows:
select * from Table t1
where not exists (select 1 from Table t2 where t2.name=t1.name and t2.date=t1.date)
(The example doesn't make a lot of sense except to illustrate the problem)
I have no control over the select part of the query.
select * from Table t1
The table can be given any alias or none at all. So therefore I have no idea how to reference the table in the where clause.
Is there a way to get around this?
(Oracle 10GR2)
I think you will need to use NOT IN:
(name, datecol) not in (select name, datecol from table2)