Ambiguous column name 'countryname' - sql

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

Related

ORA-00957: duplicate column name ERROR While Creating View

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

Selecting data using three tables only joining two tables

I am trying to select data using three tables. I need to get an equity number and contract date from an actor and contract table where the name of the film = x from a film table.
I have done:
SELECT equity_number, contract_date
FROM actor,
contract,
film
WHERE actor.equity_number = contract.equity_number
and title = 'x'
Although I am getting a column ambiguously defined error.
You don't have any Relationship with the film table.
And use a modern way of writing sql :)
SELECT a.equity_number, c.contract_date
FROM actor a
INNER JOIN contract c on a.equity_number = c.equity_number
INNER JOIN film f on f.SOMERELATIONSHIPID = c.SOMERELATIONSHIPID
WHERE f.title = 'x'
I suggest you to use JOINS instead of old comma-separated syntax. After It set aliases for each table and use these aliases in select list. Something like:
SELECT a.equity_number,
c.contract_date
FROM actor a
LEFT JOIN contract c ON a.equity_number = c.equity_number
LEFT JOIN film f ON a.film_id = f.id -- there should be related columns
AND f.title = 'x'

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

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.

Error in query: aggregate function or the GROUP BY clause

Hi all I have a problem with an SQL query: the problem is that if i add GROUP BY the database engine outputs the error:
Column 'dbo.classes.class_name' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY clause.
My query is:
string query = "SELECT p.*
FROM dbo.classes AS p INNER JOIN teacher_classes AS a
ON a.class_id = p.class_id
and teach_id = #id
GROUP BY p.class_id";
Is there any help please for that.
Note without group by the query work fine but the result not grouped.
Your query is:
SELECT p.*
FROM dbo.classes AS p INNER JOIN
teacher_classes AS a
ON a.class_id = p.class_id and teach_id = #id
GROUP BY p.class_name;
You are trying to select all the columns from p and yet you're are grouping by class_name. This is not allowed in most databases. What happens if you have two classes, but information is different from them?
One option is to use distinct rather than group by to remove duplicates:
SELECT distinct c.*
FROM dbo.classes c INNER JOIN
teacher_classes tc
ON tc.class_id = c.class_id and tc.teach_id = #id;
Another option is to use something like in to find the matching classes for the teacher:
select c.*
from classes c
where c.class_id in (select tc.class_id from teacher_classes where teach_id = #id)
Notice I also changed your aliases so they have some relationship to the table names. This makes the query much easier to read.