SQL-Duplicate column names - sql

I am using SQL developer and I am trying an outer join on two tables. The error it is showing is "Duplicate column names". I have used the table nameswhile comparison but still it is giving error. Code is as following:
CREATE VIEW OECD_VIEW AS
SELECT * FROM DM_OECD_GDP FULL OUTER JOIN DM_OECD_DOCTORS
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;
I have heard that this error can be resolved by aliasing but I don't know how to alias while comparing. Can this be done?
Thanks

You have to explicitly name returned fields from both tables and alias fields having the same name, like:
CREATE VIEW OECD_VIEW AS
SELECT DM_OECD_GDP.DATA_YEAR AS GDP_DATA_YEAR,
DM_OECD_DOCTORS.DATA_YEAR AS DOC_DATA_YEAR,
... rest of the fields here
FROM DM_OECD_GDP
FULL OUTER JOIN DM_OECD_DOCTORS
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;

This is the generalised query to create views with alias and full outer join :
create view v6 as select t1.c1, t2.c2 from t1 FULL OUTER JOIN t2 on t1.id = t2.id;

If both tables have same column names, may be the foreign keys, dont use * , Manually select the column names
table_one - ID,Name
table_two - ID,SecondName
select table_one.ID,table_one.Name,table_two.ID as ID2,
table_two.SecondName full outer join on table_one.ID=table_two.ID

Related

Create View in Athena but "column name specified more than once"

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)

Use of correct join type in bigquery

I have two tables as Table-1 and Table -2. I would like to get everything from Table 1 and only the last column from Table -2. My common key is highlighted in the picture. I've tried the following code.Please let me know if it is correct.
select t.*,t2.Link_Clicks from t1
left join
t2
on t1.date_start=t2.date
and t1.Adset=t2.Adset
and t1.CampaignName=t2.CampaignName
select t.* should be select t1.*
when you say "everything from Table 1" are you referring to columns or rows? If columns then you don't need to do left join, inner join should suffice. But if rows, then left join is right.

Qlikview left join two table and generate the data to qvd

I'm relatively new to qlikview and not sure if the left join is the same as normal sql: SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
Would like to have your assistance to left join the tables using TRIP_ID, and classified the period using the ARRIVE_DT then generate the data in qvd
LOAD "TRIP_ID",
"PM_M",
"ARRIVE_DT",
"CHASSIS_MAX_LADEN_WEIGHT_Q";
SQL SELECT *
FROM Table1."V_GATE_PM_TRIP";
LOAD "TRIP_ID",
"CREATE_DT",
"MODIFIED_DT";
SQL SELECT *
FROM Table2."V_GATE_PM_TRIP_CNTR";
Thank you.
Just add LEFT JOIN before the second load.If you loading both tables from the same SQL source then just use the SQL syntax the same as you wrote in your question
This is what i managed to do after looking at online and it generated the qvd with the data sorted by the date.
TEMP_TABLE1:
LOAD "TRIP_ID",
"PM_M",
"ARRIVE_DT";
SQL SELECT *
FROM EODWADMIN."V_GATE_PM_TRIP";
LEFT JOIN
LOAD "TRIP_ID",
"CREATE_DT",
"MODIFIED_DT";
SQL SELECT *
FROM EODWADMIN."V_GATE_PM_TRIP_CNTR";
TABLE2:
LOAD*, 1 AS FLAG
RESIDENT TEMP_TABLE1 ORDER BY ARRIVE_DT;
STORE TABLE2 INTO PM.QVD (QVD);
In QlikView the LEFT JOIN work a little different that in SQL.
In SQL you specify the fields from both table in the sentence of the left join:
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.FieldA=t2.FieldB
In QlikView the JOIN works with the field name, you don't need to specify the name. In the case the fields have different name, you have to rename one of them (or both) to get the same name:
TABLE1:
LOAD
TRIP_ID1 as JOIN_KEY,
PM_M,
ARRIVE_DT
FROM Tabel1
;
LEFT JOIN (TABLE1)
LOAD
TRIP_ID2 as JOIN_KEY,
CREATED_DT,
MODIFIED_DT
FROM Table2
;

Creating a table that includes only records that exist in one table without existing in another

I'm a newbie here and hoping someone can help with this sql. I've created two tables, one of which holds EVERY record, another which contains the records that I DON'T want in my table.
I tried joining them in the way I researched that is supposed to work, to include only records where they ARE NOT In the second table, but I'm getting an error.
The SQL is:
Create table t3 as
(Select * from t1
Left Outer join t2
on (t1.ID = t2.Orig_ID and t1.ID_Line = t2.Orig_ID_Line)
Where t2.Orig_ID is null
and t2.Orig_ID_Line is null)
This should be simple. However, i'm getting an error that says "Duplicate column name in Orig_ID"
HELP!
Thanks.
You were very close with your original statement, but forgot to limit the columns to those from t1, so you had twice as many columns as intended. Try:
CREATE TABLE t3 AS
(SELECT t1.* FROM t1 -- Key change * -> t1.*
LEFT OUTER JOIN t2
ON (t1.ID = t2.Orig_ID AND t1.ID_Line = t2.Orig_ID_Line)
WHERE t2.Orig_ID IS NULL
AND t2.Orig_ID_Line IS NULL)
Try this
Create table t3 as
select * from t1 where (t1.ID, t1.ID_Line) not in ( select t2.ID, t2.Orig_ID_Line from t2 where t2.ID is not null and t2.Orig_ID_Line is not null )
You get duplicate column name error, because you join and select both fields in both tables. So same column names return from the query and table can not have same column names. What you need is to select columns only in t1 table
select t1.* ......
But you do not need join operation. What you need is simple. Using "not in" operator is what you need. Have a look at an sql tutorial for in/not in operators.

select everything from one table, and array from another

I have one table that contains lots of data and I want to select everything from this one.
Another table contains different statuscodes ( fields ID and text ), and I want to join this table into the first one so i get something like
All data from first table id1,text2,id2,text2 or id1,id2,text1,text2 from the second table.
USe LEFT OUTER JOIN
Select T1.*,T2.ID,T2.Text
from firstTable t1
LEFT OUTER JOIN SecondTable T2
on T1.<col>=T2.<col>
You can use join like this whether it may be left/right/inner join
SELECT t1.*,t2.id,t2.text
FROM table1 t1
LEFT JOIN table2 t2
ON t2.column_name=t1.column_name