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.
Related
I have created a SELECT query that joins three tables in AWS Athena - the query itself works...
Select t1.*, t2.*, t3.*
from "analytics_poc"."stg_orderitem" as t1
INNER Join "analytics_poc"."stg_orderitemtag" as t2
ON t1.orderitemid=t2.orderitemid
LEFT Join "analytics_poc"."stg_tag" as t3
ON t3.tagid=t2.tagid
However, when I try to create a VIEW from this query I get this error...
CREATE OR REPLACE VIEW "CMS_orderitem_tags"
AS
Select t1.*, t2.*, t3.*
from "analytics_poc"."stg_orderitem" as t1
INNER Join "analytics_poc"."stg_orderitemtag" as t2
ON t1.orderitemid=t2.orderitemid
LEFT Join "analytics_poc"."stg_tag" as t3
ON t3.tagid=t2.tagid
line 1:1: Column name 'orderitemid' specified more than once.
Does anyone know why this is the case?
It would appear that a column called orderitemid exists in more than one of those tables.
The SELECT command doesn't mind having multiple output columns with the same name, but CREATE VIEW doesn't allow it.
You could try USING instead, which removes the 'join' column.
Change this line:
INNER Join "analytics_poc"."stg_orderitemtag" as t2 ON t1.orderitemid=t2.orderitemid
into:
INNER Join "analytics_poc"."stg_orderitemtag" as t2 ON USING(orderitemid)
I tried to delete an entry from table1 based on criteria on table2. (id in table1 is foreign key from table2)
I tried all those below and all returned with syntax errors.
take 1:
delete table1.* from table1 inner join table2 on table1.id=table2.id where table2.column3=21 and table2.column4=59;
Error: near "table1": syntax error
take 2:
delete table1 from table1 inner join table2 on table1.id=table2.id where table2.column3=21 and table2.column4=59;
Error: near "table1": syntax error
take 3:
delete from table1 inner join table2 on table1.id=table2.id where table2.column3=21 and table2.column4=59;
Error: near "inner": syntax error
Anyone knows what are the correct syntax?
If this is the wrong approach, what is the correct way to achieve my goal?
Really appreciate it.
No Join in SqlLite DELETE, by the look of it, but you could use exists:
delete
from table1
where exists
(select 1
from table2
where table1.id=table2.id
and table2.column3=21
and table2.column4=59
);
Since SQLite does not support JOIN yet in outer query of DELETE statements, consider using subquery with IN or EXISTS:
DELETE FROM table1
WHERE id IN (
SELECT id
FROM table2
WHERE table2.column3 = 21
AND table2.column4 = 59
)
DELETE FROM table1
WHERE EXISTS (
SELECT 1
FROM table2
WHERE table1.id = table.id
AND table2.column3 = 21
AND table2.column4 = 59
)
I have the following SQL code (this is how much I've got so far):
MERGE INTO SCHEMA1.TABLE_1 table1 USING
(
SELECT DISTINCT table2.column1,
view1.column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
) t2 ON (table1.column3 = t2.column1 )
WHEN MATCHED THEN
UPDATE
SET table1.column4 = t2.column2;
The following is the definition of VIEW_1 :
CREATE VIEW SCHEMA_2.VIEW_1
AS (SELECT
SCHEMA_2.TABLE_1.COLUMN_1,
SCHEMA_2.TABLE_2.COLUMN_1,
SCHEMA_2.TABLE_2.COLUMN_2,
SCHEMA_2.TABLE_2.COLUMN_3,
SCHEMA_2.TABLE_5.COLUMN_1,
SCHEMA_2.TABLE_6.COLUMN_1,
SCHEMA_2.TABLE_6.COLUMN_2,
SCHEMA_2.TABLE_6.COLUMN_3,
SCHEMA_2.TABLE_6.COLUMN_4,
SCHEMA_2.TABLE_7.COLUMN_1,
SCHEMA_2.TABLE_7.COLUMN_2,
SCHEMA_2.TABLE_8.COLUMN_1
FROM SCHEMA_2.TABLE_1
INNER JOIN SCHEMA_2.TABLE_2
ON SCHEMA_2.TABLE_1.COLUMN_1 = SCHEMA_2.TABLE_2.COLUMN_2
INNER JOIN SCHEMA_2.TABLE_5
ON SCHEMA_2.TABLE_1.COLUMN_4 = SCHEMA_2.TABLE_5.COLUMN_3
LEFT OUTER JOIN SCHEMA_2.TABLE_6
ON SCHEMA_2.TABLE_2.COLUMN_2 = SCHEMA_2.TABLE_6.COLUMN_4
LEFT OUTER JOIN SCHEMA_2.TABLE_7
ON SCHEMA_2.TABLE_2.COLUMN_1 = SCHEMA_2.TABLE_8.COLUMN_5
);
But I'm getting the below error message:
Error report -
SQL Error: ORA-30926: unable to get a stable set of rows in the source tables
30926. 00000 - "unable to get a stable set of rows in the source tables"
*Cause: A stable set of rows could not be got because of large dml
What causes the error? Where to change in the code to make it work?
Thanks for helping out!
For this example your problem is definitely in the USING subquery. This query produces more than one value of table2.column1:
SELECT DISTINCT table2.column1,
view1.column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
So the ON clause will match the same row(s) in table1 more than once:
ON (table1.column3 = t2.column1 )
Oracle cannot figure out which value of t2.column2 should be used in the UPDATE, so it hurls ORA-30926.
Using distinct in the subquery doesn't help because that gives permutations of all the columns. You need to write a subquery which will produce unique values of t2.column1 across all rows, or add another identifying column(s) to generate a unique key you can join to table1.
In my experience, this error is returned, not only when the USING clause returns more than one row for a row in the MATCH table, but also frequently when it cannot be sure that only one row will be returned (even if there are no actual cases of multiple rows being returned). To force the parser to accept the query in cases like this, I usually resort to using a GROUP BY on the MATCH..ON column(s).
MERGE INTO SCHEMA1.TABLE_1 table1 USING
(
SELECT table2.column1,
MAX(view1.column2) as column2
FROM SCHEMA2.TABLE_2 table2
LEFT JOIN SCHEMA2.VIEW_1 view1
ON table2.column2 = view1.column3
GROUP BY table2.column1
) t2 ON (table1.column3 = t2.column1 )
WHEN MATCHED THEN
UPDATE
SET table1.column4 = t2.column2;
I am working on an SSIS job that contains a complex query.
It has some thing like :
some sql statements
left outer join
(
select query joining two more tables )
table1
here, i am unable to understand what that table1 mean ? Is it a kind of temporary view
created . This table1 is used in the other parts of query . But, actually the table1 does
exists in the database.
Is it like , the results of the select query in the parenthesis is created as table1
Please clarify me on this..
I am not able to put down my code because of Security Policies
Here is SQL Fiddel example
Below is the sample query
Select Temp1.id,Table1.id Table1_id
from Temp1
left Outer join
(
Select Temp2.id
from Temp2
join Temp3
On Temp2.id = Temp3.id
) Table1
on Temp1.id = Table1.Id
In above example table1 is the Alias for data coming from joinsof two tables (temp2 and temp3)
table1 is an alisas your subquery. It's the name of subquery you can use with columns for example table1.col1
It is an alias for the query in the parenthesis.
If you would remove that you would get an error.
Aliases are also good when you have the same column in more than on joined tables, so you can distinquish them.
For instance if colX is both in Table1 and Table2 you would have a query like:
SELECT T1.colX,T2.colX
FROM Table1 T1
JOIN Table2 T2
ON T1.id = T2.id
I tried doing this but it failed.
SELECT table2.ID, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
How do you select all columns from a table?
EDIT: There is no error in the above query. I don't know what caused the error but the code is now working.
You had field names conflict as both tables have ID field. You must to
SELECT table2.ID as t2_id, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
What you have is syntactically correct, exactly what did you mean by it failed? Did you get an error message or just not the results you wanted? (BTW it is a bad practice to select *, only return the columns you need. In this case you do not need all the columns as the id field in table1 will have the exact same data as the file din table 2 it is joined to)
SELECT t2.ID, t1.* FROM table2 t2
LEFT JOIN table1 t1 ON t1.ID = t2.table1ID
this works on sql 2000+
If I am working inside a stored procedure where I have a defined #Table data type, there is no issue with using select table.* especially if I am using it for an output SELECT at the end. So the comment about production servers and network traffic in this case is meaningless as the entire stored procedure executes in memory. A select.* in this case is merely returning all the columns which have been defined ahead of time.