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

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)

Related

Database select all data with table qualifier in PostgreSQL

I need to select from a database some tables with many columns. So I want to select
select t1.*,t2.*,t3.*
from table1 t1
left outer join table2 t2 on ...
left outer join tabl 3 t3 on ...
...
Some keys are identically named, but - due to the outer command - sometimes null. Unfortunately I cannot directly see whether the key comes from t1, t2 or t3. Is there any chance to add automatically the tablenames or another separating/distinguishing qualifier to all column names, i.e. t1_thekey, t2_thekey, t3_thekey ...?
Postgres doesn't allow you to rename the columns en masse. However, you could select the columns as records, so you can see where they come from:
select t1, t2, t3

SQL-Duplicate column names

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

SQL join to return a table with multiple columns from other tables replacing its own

I am trying to write an SQL query that will return Table1, which has 10 columns. This table consists of a primary key id, 4 foreign key Id columns, and 5 other columns that I want to return but not change. The goal is to do a join to replace the foreign key Ids with their descriptions that are held in other tables.
Here is one attempt with the first FK Id:
Select * from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
This left join returns the description from table2, but does not replace it.
Here is another with the first FK Id:
Select t2.BranchName from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
This returns the name I want, but does not return table1 fully.
For the sake of an example you could pretend that OtherName3, OtherName4, OtherName5 are in tables Table3, Table4, Table5, respectively.
This may seem trivial for experienced SQL devs, but I am having a hard time figuring out the syntax.
Thanks!
I'm not sure what you mean by replace it.
I think you just need to list out all the columns you want:
Select t1.col1, t1.col2, t1.col3, . . .,
t2.name
from Table1 t1 left join
Table2 t2
on t1.BranchId = t2.BranchId;
I don't know what you mean by 'replace' but you just need to qualify what columns from which table you want. That goes for all tables you are joined to, especially if they have the same column name in multiple tables. I put junk columns in since I don't know your tables but you should get the general idea.
Select t2.BranchName, t1.BranchId, t1.Name, t1.Amount, t2.BranchLocation from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
I think this is what you are looking for:
select t1.*, t2.BranchName from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
Return Table1 fully (all columns) and only the description (BranchName) from Table2.
If using SQL Server, see all syntax options for the SELECT clause here:
https://msdn.microsoft.com/en-us/library/ms176104.aspx

How to select records that do not exist in two (or more) tables

I have 3 tables of accounts that all contain the same fields. Table1 contains all accounts while Table2 and Table3 contain subsets of the accounts. I'm trying to select records in Table1 that do no exist in Table2 or Table3.
Let's say the table layout is like this and is the same for all 3 tables:
|AcctNum|Name|State|
I know how to do this if it was just Table1 and Table2, using a left join and Is Null, but the 3rd table is throwing me. Is this possible to do in one query? Can you combine left joins? I should point out I'm using Access 2010.
Yes you can combine left joins and with the odd syntax Access uses the query should look like this:
SELECT T1.AcctNum
FROM (Table1 AS T1 LEFT JOIN Table2 AS T2 ON T1.AcctNum = T2.AcctNum)
LEFT JOIN Table3 AS T3 ON T1.AcctNum = T3.AcctNum
WHERE (((T2.AcctNum) Is Null) AND ((T3.AcctNum) Is Null));
You can use Access to create a view called TableCombined that is a union of both Table2 and Table3.
At that point, you can use your left join and Is Null query and join TableCombined to Table1.
Hope this helps!
You can also do a NOT EXISTS statement which makes sense logically for what you are trying to achieve.
For example:
SELECT ACCTNUM
FROM TABLE1
WHERE NOT EXISTS (SELECT TABLE2.ACCTNUM FROM TABLE2 INNER JOIN TABLE3 WHERE TABLE2.ACCTNUM IS NULL AND TABLE3.ACCTNUM IS NULL)

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