ORA-00957: duplicate column name ERROR While Creating View - sql

I am getting ORA-00957: duplicate column name error while trying to create view. What is the problem ? How can I fix my query?
CREATE VIEW SIGN
AS
SELECT
CONTRACTS.CONTRACT_ID, CONTRACTS.PERSONNEL_ID, CONTRACTS.CUSTOMER_ID,
CUSTOMERS.FIRST_NAME,
PERSONNEL.FIRST_NAME, CUSTOMERS.CUSTOMER_ID, PERSONNEL.PERSONNEL_ID
FROM
CONTRACTS, CUSTOMERS, PERSONNEL
WHERE
(CONTRACTS.PERSONNEL_ID = PERSONNEL.PERSONNEL_ID)
AND (CONTRACTS.CUSTOMER_ID = CUSTOMERS.CUSTOMER_ID)

First, learn to use proper, explicit, standard, readable JOIN syntax.
Second, views cannot have duplicates names, such as PERSONNEL_ID and FIRST_NAME in your view.
Third, table aliases make the query easier to write and read.
So:
CREATE VIEW SIGN AS
SELECT c.CONTRACT_ID, c.PERSONNEL_ID, c.CUSTOMER_ID,
cu.FIRST_NAME,
p.FIRST_NAME as PERSONNEL_FIRST_NAME
FROM CONTRACTS C JOIN
CUSTOMERS CU
ON C.CUSTOMER_ID = CU.CUSTOMER_ID JOIN
PERSONNEL P
ON C.PERSONNEL_ID = p.PERSONNEL_ID;

Column names in a result set must be unique. Qualifying them with a table name does not make the column names any different, so
SELECT CONTRACTS.PERSONNEL_ID, PERSONNEL.PERSONNEL_ID...
will fail because PERSONNEL_ID is specified twice.
The following should work as you expect:
CREATE VIEW SIGN AS
SELECT co.CONTRACT_ID,
co.PERSONNEL_ID,
cu.CUSTOMER_ID,
p.FIRST_NAME
FROM CONTRACTS co
INNER JOIN CUSTOMERS cu
ON cu.CUSTOMER_ID = co.CUSTOMER_ID
INNER JOIN PERSONNEL p
ON p.PERSONNEL_ID = co.PERSONNEL_ID

Related

To create a view using 5 tables with all columns of all tables

I have to create a view that joins together all of the columns in the CUSTOMERS, ORDERS, ORDERDETAILS, EMPLOYEES, PAYMENTS and PRODUCTS tables.
the schema for the table is below
I tried the following query, though I am at a loss how to solve the above question :
create view orders_view AS
select *
from sys.customers c
left JOIN EMPLOYEES e
on c.SALESREPEMPLOYEENUMBER = e.EMPLOYEENUMBER
left join sys.orders o
on c.CUSTOMER NUMBER = o.CUSTOMERNUMBER
left join sys.orderdetails od
on o.ORDERNUMBER = od.ORDERNUMBER
left join sys.products p
on od.PRODUCTCODE = p.PRODUCTCODE
left join sys.PAYMENTS py
on c.CUSTOMERNUMBER = py.customernumber
I am a newbie with SQL and databases, so any help is appreciated.
Here are some observations on things going wrong:
You have curly braces that are not necessary (perhaps this is a typo in the question).
You are selecting *, but have duplicate column names (such as productcode), which prevents the view from being created. Best practice: list all the columns explicitly in the view.
You have c.CUSTOMER NUMBER = o.CUSTOMERNUMBER. The space is probably a typo. If not, change the name so the space is not part of the name. Best practice: Use identifiers that do not have to be escaped.
I am not aware of a sys.customers table. The sys schema should only be used for internal Oracle objects. (Here is one source.)
thank you for all the inputs. they helped me a lot in figuring out the answer.
Following is the query for the question asked above.it gave me a view with columns from all tables.
create or replace view overall AS
select c.*,
e.LASTNAME,
e.FIRSTNAME,
e.EXTENSION,
e.EMAIL,
e.OFFICECODE,
e.REPORTSTO,
e.JOBTITLE,
o.ORDERNUMBER,
o.ORDERDATE,
o.REQUIREDDATE,
o.SHIPPEDDATE,
o.STATUS,
o.COMMENTS,
od.PRODUCTCODE,
od.QUANTITYORDERED,
od.PRICEEACH,
od.ORDERLINENUMBER,
p.PRODUCTNAME,
p.PRODUCTLINE,
p.PRODUCTSCALE,
p.PRODUCTVENDOR,
p.PRODUCTDESCRIPTION,
p.QUANTITYINSTOCK,
p.BUYPRICE,
p.MSRP,
py.CHECKNUMBER,
py.PAYMENTDATE,
py.AMOUNT
from sys.customers c
left JOIN EMPLOYEES e
on c.SALESREPEMPLOYEENUMBER = e.EMPLOYEENUMBER
left join sys.orders o
on c.CUSTOMERNUMBER = o.CUSTOMERNUMBER
left join sys.orderdetails od
on o.ORDERNUMBER = od.ORDERNUMBER
left join sys.products p
on od.PRODUCTCODE = p.PRODUCTCODE
left join sys.PAYMENTS py
on c.CUSTOMERNUMBER = py.customernumber
;
Your query looks alright, but I don't think it will let you create a view if more than one column has the same name.
Since there are duplicates, e.g. CITY, I think the only way around it is to name all the columns and give unique names for duplicate columns.

SQL Server query issue - ambiguous column

I have four tables :
Applicant (aid, aname)
entrance_test (Etid, etname)
etest_centre (etcid, location)
etest_details (aid, etid, etcid, etest_dt)
I want to select the number of applicants who have appeared for each test, test center wise.
This is my current query:
select
location, etname, count(Aid) as number of applicants
from
applicant as a
inner join
etest_details as d on a.aid = d.aid
inner join
Entrance_Test as t on t.Etid = d.Etid
inner join
Etest_Centre as c on c.Etcid = d.Etcid
group by
Location, Etname
This is the error I am getting :
Ambiguous column name 'Aid'
You have the column aid in multiple tables, and it doesn't know which to pick from. You should specify which table it is from using the aliases you defined.
In this case, since a.Aid is the same as d.Aid (due to the JOIN), I'm using the a alias, but do keep in mind if location and etname also appear in multiple tables, you need to specify which table it should pick from.
Select c.location, t.etname, Count(a.Aid)
From Applicant As a
Inner Join etest_details As d On a.aid = d.aid
Inner Join Entrance_Test As t On t.Etid = d.Etid
Inner Join Etest_Centre As c On c.Etcid = d.Etcid
Group By c.Location, t.Etname
As a rule of thumb, when you have multiple sources in one query, you should always be explicit about which table it should come from. Even if you're sure it only exists in one of them, it's a good habit to get into to avoid issues like this in the future.
You need to mention the alias in the COUNT clause. Since you are using aliases, it would be better if you use them in the SELECT and GROUP BY sections as well. In this case, it should be :
SELECT a.location,
a.etname,
COUNT(d.Aid)
FROM applicant AS a
INNER JOIN etest_details AS d ON a.aid = d.aid
INNER JOIN Entrance_Test AS t ON t.Etid = d.Etid
INNER JOIN Etest_Centre AS c ON c.Etcid = d.Etcid
GROUP BY a.Location,
a.Etname

Ambiguous column name 'countryname'

I have written a procedure like below
BEGIN
SELECT distinct countryname, countryid
FROM Country c
Inner join Cause cs on cs.CountryName = c.CountryName
END
It is displaying error message like "Ambiguous column name 'countryname'."
I want to fetch distinct rows on the basis of countryname and countryid.
Please help...
If in your query the same column-name is available on multiple tables the Interpreter needs to know which column is meant.
In your case you should write your query like this:
SELECT DISTINCT c.countryname, countryid
FROM Country c INNER JOIN Cause cs
ON cs.CountryName = c.CountryName
or
SELECT DISTINCT cs.countryname, countryid
FROM Country c INNER JOIN Cause cs
ON cs.CountryName = c.CountryName
Suggestions:
You may want to specify the alias on all columns always
Design your model to be joinable on unique integer or guid indexes, avoid joining on "countrynames" unless you can guarantee 100% that there will never be spelling errors (and even then it's bad design).
You shall specify table in select. As example:
BEGIN
SELECT distinct c.countryname, c.countryid
FROM Country c
Inner join Cause cs on cs.CountryName = c.CountryName
END
When you use alias for tables make sure that the fields for each tables are represented by their respective alias.
For example you have used alias c for country table and cs for cause, so in order to use any columns from country table use c.column_name.
Your error message is displayed because there might be same column name in both table and the sql-server doesn't know which column from which table are you referring to.
So your code should be like
BEGIN
SELECT distinct c.countryname, c.countryid
FROM Country c
Inner join Cause cs on cs.CountryName = c.CountryName
END

Query with columns from 4 tables in SQL

Can anyone who knows SQL, specifically the flavor used in Microsoft Access 2013, tell me what I'm doing wrong here?
SELECT custid, custname, ordno, itemno, itemname
FROM cust
INNER JOIN order
ON cust.custid = order.custid
INNER JOIN orderitems
ON order.ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
I've already read other, similar questions, and tried the methods they used in their solutions, but I'm getting a "Syntax error in FROM clause.", almost no matter what I try.
* * *
SOLUTION: Thanks for the replies! In addition to adding square brackets around "order" and using TableName.ColumnName syntax in SELECT, I had to use parentheses for my multiple INNER JOINs. Here is the fixed code:
SELECT cust.custid, cust.custname, [order].ordno, orderitems.itemno, inv.itemname
FROM ((cust
INNER JOIN [order]
ON cust.custid = [order].custid)
INNER JOIN orderitems
ON [order].ordno = orderitems.ordno)
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
SELECT cust.custid --<-- Use two part name here
,cust.custname
,[order].ordno
,orderitems.itemno --<-- Only guessing here use the correct table name
,inv.itemname --<-- Only guessing here use the correct table name
FROM cust
INNER JOIN [order]
ON cust.custid = [order].custid --<-- used square brackets [] around ORDER as it is
INNER JOIN orderitems -- a key word.
ON [order].ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
In your Select Statament you need to use Two Part name i.e TableName.ColumnName since these column can exist in more than one Tables in your FROM clause you need to tell sql server that columns in your select coming from which table in your from clause.

What is wrong with my join in this query?

Im practicing basic SQL with this site http://www.sqlishard.com/Exercise
Here is the question:
S5.0 - INNER JOIN
Now that we can pull data out of a single table and qualify column
names, let's take it a step further. JOIN statements allow us to
'join' the rows of several tables together using a condition to define
how they match one another. SELECT [columns] FROM FirstTable INNER
JOIN SecondTable ON FirstTable.Id = SecondTable.FirstTableId
Try using the INNER JOIN syntax to SELECT all columns from the
Customers and Orders tables where the CustomerId column in Orders
matches the Id column in Customers. Since both tables have an Id
column, you will need to qualify the Customers id in the WHERE clause
with either the table name or a table alias.
Here is my answer:
SELECT *
FROM Customers AS c
INNER JOIN Orders AS o ON c.ID = o.ID
WHERE o.CustomerID = c.ID
The site says im wrong? Could anyone explain where i'm going wrong?
EDIT: I see now I dont need the WHERE clause, but the question states..
you will need to qualify the Customers id in the WHERE clause with
either the table name or a table alias.
Hence my confusion. Thanks none the less.
Try this way:
SELECT c.ID,o.ID
FROM Customers AS c
INNER JOIN Orders AS o ON o.CustomerID = c.ID
or using where clause
SELECT *
FROM Customers AS c, Orders AS o
where o.CustomerID = c.ID
If you use JOIN.. ON, you do not need where clause