I have a more complex query with inner join that throws the error:
[Error Code: -206, SQL State: 42703] DB2 SQL Error: SQLCODE=-206,
SQLSTATE=42703, SQLERRMC=TT.ALIAS, DRIVER=4.13.127. 2) [Error Code:
-727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-206;42703;TT.ALIAS, DRIVER=4.13.127
the simple version of the query that doesn't work is:
select column1 as alias from table1 tt
inner join table1 ts
on tt.alias = ts.column1
this simple query doesn't make sense but is a simple version of the query I'm trying to run.
This was supposed to run with no errors right? If I remove the alias it runs with no problem..
Thanks in advance
You cannot use a column alias in an on or where clause. You can do:
select column1 as alias
from table1 tt inner join
table1 ts
on tt.column1 = ts.column1;
You could also do:
select alias
from (select column1 as alias
from table1
) tt inner join
table1 ts
on tt.alias = ts.column1;
During the compile phase, the queries are evaluated by clause, with the from clause first, then then where and select. In other words, the compiler doesn't "know" what is in the select when it evaluates the from.
Related
Getting error "Each function argument is an expression, not a query; to use a query as an expression, the query must be wrapped with additional parentheses to make it a scalar subquery expression at"
While running query in Bigquery SQL workspace it's working fine. But as a shell script it is showing above error
So what's the possible explanation.
Point to note:
Left join ( select col , vol1 from table)
Left join ( select col as Col as C, vol1 as V from table)
And
Left join ( select col , vol1 from table) t1 instead of as t1
I am joining 2 tables and using <= in Join Condition also applying one filter condition so that it can only fetch remaining data from left table where filter condition is true.
I am using below query.
SELECT * FROM test.TABLE1 T1
left join test.TABLE2 T2
on (
T1.low<=T2.low
)
where t1.ID='1';
Error:
Error while compiling statement: FAILED: SemanticException
[Error 10017]: Line 4:0 Both left and right aliases encountered in JOIN '0' (state=42000,code=10017)
When I am only giving '=' condition instead of <= then its running without any issue.
You could do it like this:
with data as ( Select * FROM test.TABLE1 t1 WHERE t1.ID='1')
Select *
FROM data T1
LEFT JOIN test.TABLE2 T2 on T1.low<=T2.low
I get the error
[Code: 10002, SQL State: 42000]
Error while compiling statement: FAILED: SemanticException [Error 10002]: Line 11:60 Invalid column reference 'FIELD3'
when running the following query:
CREATE TABLE NEW_TABLE
AS
SELECT
T1.*,
T2.*,
T3.*
FROM
OLD_TABLE1 T1
LEFT JOIN
OLD_TABLE2 T2 ON (T1.FIELD1 = T2.FIELD1)
LEFT JOIN
OLD_TABLE3 T3 ON (T1.FIELD3 = T3.FIELD3);
What is the potential issue of the above query? I double checked that the FIELD3 exists in OLD_TABLE1 and OLD_TABLE3.
One major issue you have is that duplicated column names in the table. At the very least, the JOIN conditions will introduce duplicates. There might be other duplicates as well.
List the columns you want explicitly:
CREATE TABLE NEW_TABLE AS
SELECT . . . -- list column names explicitly
FROM OLD_TABLE1 T1 LEFT JOIN
OLD_TABLE2 T2
ON T1.FIELD1 = T2.FIELD1 LEFT JOIN
OLD_TABLE3 T3
ON T1.FIELD3 = T3.FIELD3;
This problem may occur with CREATE TABLE. That said, it is more common with aggregation queries.
Here is my query:
SELECT
t2.*
FROM
(
SELECT
FullName
FROM
pr
GROUP BY
FullName
HAVING
COUNT(*)>=2
) T1
JOIN
pr T2 ON T1.FullName = T2.FullName;
I used this answer to build this: SQL Return only duplicate rows.
Although they didn't specify which DBMS they were using, I can assume it was not MS Access since it ran properly for them and not me.
Whenever I try to save this query, it says:
Syntax error in FROM clause
Not sure where this is wrong. I know that access requires some weird brackets during joins but the problem is apparently with one of the FROM statements. Any ideas here?
I would expect an MS Access query to look like this:
SELECT t2.*
FROM (SELECT FullName
FROM pr
GROUP BY FullName
HAVING COUNT(*) >= 2
) AS T1 INNER JOIN
pr as T2
ON T1.FullName = T2.FullName;
Note the ass and INNER.
I have 2 tables:
I want to update my table1 records with the suitable age, which can be found in table2. Unique identifier is the BVD_ID_NUMBER. I tried to do this using the following code
UPDATE table1
SET table1.age =
(select table2.age2
from
(select distinct table2.BVD_ID_NUMBER, table2.age2
FROM table1
inner JOIN table2
on table1.ACQUIROR_BVD_ID_NUMBER=table2.BVD_ID_NUMBER)
where table2.BVD_ID_NUMBER=table1.ACQUIROR_BVD_ID_NUMBER);
I received the following error:
SQL Error: ORA-00904: "ORBIS_DISTINCT"."BVD_ID_NUMBER": invalid identifier
00904. 00000 - "%s: invalid identifier"
Any help?
Hmmm. You have overcomplicated your query. Usually, when using correlated subqueries, there is no reason to mention the outer table in the inner subquery. Oracle doesn't allow scoping beyond one level for correlated subqueries, so you need to simplify for the correlation clause:
UPDATE table1 t1
SET age = (select t2.age2
from table2 t2
where t1.ACQUIROR_BVD_ID_NUMBER = t2.BVD_ID_NUMBER
);
This is likely to cause a "subquery returns more than one row" type of error. To fix that, use aggregation or rownum = 1:
UPDATE table1 t1
SET age = (select t2.age2
from table2 t2
where t1.ACQUIROR_BVD_ID_NUMBER = t2.BVD_ID_NUMBER and
rownum = 1
);