(SQL) How do I differentiate 2 columns from different tables with the same name when selecting? - sql

I'm using an Oracle 12c SQL server. The goal is to create a view containing each company and the drugs it produces.
How can I differentiate two columns with the exact same name but located in different tables using SELECT?
All relevant code below, including results with error.
I understand why I might be getting a duplicate name error as they both have the same header "name", but thought I handled it by identifying the table beforehand (i.e. pc.name and dg.name). Help!
SQL Tables Being Joined:
SQL Column Naming Error:

You have ambiguous column names in output from your view:
pc.name, dg.name
Adding alias for columns should solve this:
pc.name as pc_name, dg.name as dg_name

Related

SQLite column in table has same name as table

Hi there I have a database movies that contains a table country, with a column also called country. when attempting to use this column I have tried
country."country"
country.country
country FROM country
screenshot of my screen showing that there is a table named country and a column also called country, with the error in the right hand sidebar:
but the fact that they are the same name just keeps throwing an error. I am about to put my head through a wall I'm getting so frustrated. I have combed google and stack, but they just require that the table not have the same name as the database. I cannot seem to find a fix for this, any help is appreciated, and I am sorry if this has indeed been posted and answered elsewhere and I somehow missed it.
This is not about the table/column name: having a table column with the same name as the table it belongs to is unambiguous in SQL, since the syntax is clear enough about where a column name or a table name is expected.
Table country is not part of the from clause of your query, hence it is not available in the select clause.
You need to join that table, presumably with the incountry table:
inner join country on country.cid = incountry.cid
Then you can add country.country to the select clause.

Power Pivot relationships

Trying to create relationships (joins) between tables in power pivot.
Got 2 tables I wold like to join together, connected with a common column = CustomerID.
One is a Fact Table the other Dim table (look up).
I have run the "remove duplicates" on both tables without any problem.
But I still get an error saying : "the relationship cannot be created because each column contains duplicate values. Select at least one column that contains only unique values".
The Fact Table contains duplicates (as it should?) and the Dim Table do not, why do I get this error?
Help much appreciated
Created an appended table with both columns "CustomerID". After the columns where appended together I could "remove duplicates" and connect the tables together through the newly created appended table.
Don't know if this causes another problem later however.
You can also check for duplicate id values in a column by using the group by feature.
Remove all columns except ID, add a column that consists only of the number 1.
Group by ID, summing the content of the added column and filter out IDs whose total equals 1. What's left are duplicated IDs.

T-SQL - Using Column Name in where condition without any references when having multiple tables that are engaged using multiple joins

I am a newbie for T-Sql, I came across a SP where multiple tables are engaged using multiple joins but the where clause contain a column field without any table reference and assigned for an incoming variable,like
where 'UserId = #UserId'
instead - no table reference like
'a.UserId = #Userid'`
Can any please do refer to me any material that clears my mind regarding such issue.
If the query works it means that there is only one Column with the name UserId, if there are multiple columns with the same name you have to reference the table too.
If you don't specify the table reference you will get
Ambiguous column name 'UserId'. error
Which means there are more then 2 tables with a column name UserId.
Anyway, always try and use the reference table.

Need HELP! with - SQL Plus Select multiple columns

Hi I am using sql plus and I need to select 5 columns from 4 tables and show the result. This is the code that i used and the error I get.
SELECT CustomerID, OrderID, AircraftID, Quantity, TotalCost
FROM Customer_Table, Order_Table, Aircraft_Table, Orderline;
ERROR at line 1: ORA-00918: column ambiguously defined
What is the code to get these columns and display them from multiple tables. Please help with this.
The reason is that you tried to execute an SQL statement that joined two or more tables, where a column with the same name exists in both tables.
Exemple with this SQL query
SELECT suppliers.supplier_id, quantity
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
Since the supplier_id column exists in both the suppliers and orders table, you need to prefix the column with the table name as follows:
Moreover you need to link your tables between them. If I take the example above you should do this kind of thing
WHERE suppliers.supplier_id = orders.supplier_id;
Means you link the table suppliers with the table orders by the column supplier_id
This article may help you to understand about your issue:
http://www.sitepoint.com/understanding-sql-joins-mysql-database/
I don't know SQL plus specifically but in most SQL variants you should specify a table name then a column name. Such as SELECT Customer_Table.CustomerID ...... this will disambiguate which column should be used in the result projection.
The error means one or more of those columns exist with the same name in those tables. Thats why it does not know which column to select from what table. If you could specify the table name and then the column, I think this should go away.

Can alias in SQL be used within the same table?

I have difficulty understanding alias. Can alias in SQL be used within the same table?
In a query, you can use multiple aliases for a single table:
SELECT alias1.Name, alias2.Name
FROM table as alias1
INNER JOIN table as alias2
ON alias1.ChildId = alias2.Id
In the code above I am aliasing table as alias1 and alias2. It is the same table, with 2 different aliases.
Not sure I understand your question completely...
A good read on aliases # http://www.w3schools.com/sql/sql_alias.asp
http://www.sqltutorial.org/sqlalias.aspx
There are 2 kinds of aliases, one for tables and one for columns. Aliaes are used as a way to make your sql code more readable. It can give meaningful names to column and table names that might be long and/or confusing.
check the w3schools breif description and examples for SQL Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.
Which alias as you referring to: 'table alias' or 'column alias'?
In the SQL-92 Standard, the vernacular 'table alias' is referred to as a correlation name. A correlation name much be unique within its scope. The actual wording is as follows:
An identifier that is a correlation
name is associated with a table
within a particular scope. The scope
of a correlation name is either a
select statement: single row,
subquery, or query specification.
Scopes may be nested. In different
scopes, the same correlation name
may be associated with different
tables or with the same table.
In the SQL-92 Standard, the vernacular 'column alias' is referred to (rather wordily) as an as clause that contains a column name. There is no general condition that the same column name shall not be specified more than once in column lists (but there are context-specific restrictions e.g. a view column list). In fact, SQL's allowance of duplicate column names is often cited as a fatal flaw as regards being turly relational.