So I have looked into why this error occurs but I couldn't find any helpful answer. One person answered for another question that its because in the sql statement, the column name used for multiple tables may be the same. However, I tried to change that and yet I got the same error. Here is my sql statement:-
SELECT CLASSID INTO V_ID FROM CLASSES INNER JOIN BOOKING ON BOOKING.CLASSID=CLASSES.CLASSID WHERE BOOKING.BOOKINGID=:NEW.BOOKING_ID;
What did I do wrong?
SELECT
Bo.CLASSID INTO V_ID
FROM CLASSES cl INNER JOIN BOOKING bo ON
BO.CLASSID=Cl.CLASSID WHERE
BO.BOOKINGID=:NEWBOOKING_ID;
Try this. Your classid column exist in both tables. You have to explicitly choose classid column of which table you want select. In your select list in place of classid you should write booking.classid or classess.classid.
I added alias for table names. Bo for booking and cl for classess. This helps make code more compact. And in place of table name i have written alias name.
May be :
SELECT BOOKING.CLASSID INTO V_ID FROM CLASSES INNER JOIN BOOKING ON BOOKING.CLASSID=CLASSES.CLASSID WHERE BOOKING.BOOKINGID=:NEW.BOOKING_ID
Related
select StaffNo, SName, BranchName from STAFF inner join BRANCH on staff.branchno = Branch.branchno;
I am trying to display BranchNo in the results from the above query, whenever I add BranchNo after branch name like this.
select StaffNo, SName, BranchName, Branchno from STAFF inner join BRANCH on staff.branchno = Branch.branchno;
I get this error code ORA-00918: column ambiguously defined. To my understanding it is something to do with the fact that BranchNo is my primary key, is there a way I can get around this and display BranchNo in the query results? Many thanks.
Just prefix the ambiguous column name with the table it belongs to.
Actually, you should always prefix the columns in multi-table queries with the relevant table names; this makes the queries unambiguous for your database and for the readers
(and future maintainers) of the query.
For this, table aliases come handy (they make the query shorter).
Here is your updated query - I made a few assumptions on from which table each column comes from, that you might need to review:
select
s.staffno,
s.sname,
b.branchname,
s.branchno
from staff s
inner join branch b on s.branchno = b.branchno;
Ok, so I have been looking everywhere and i cant seem to find anything.
Heres what Im trying to do:
SELECT SFirstName, SLastName
FROM Advisors
WHERE Students <= ('1') and Students.AdvisorID=Advisors.AdvisorID;
The SName is student names, and i need to basically list advisors based on the number of active students for each, and then filter out any with more than one student.
Heres an attempt to code this with the DBO names and all.
SELECT AFirstName, ALastName
FROM DBO.Students, DBO.Advisors
WHERE Advisor.AdvisorID=Student.AdvisorID AND >1;
Basically the advisorID is a Foreign Key within the Students table, and I need to match it with the advisor table and then the >1 statement. I dont know how to reduce results by number yet however.
Whenever I try this it tells me it cannot find the advisor.advisorID or the student.AdvisorID. how would i do this while still using the foreign key from one and the primary key from the other to cross check matches.
If I understand you question correctly, you want to list the Advisors with less than 1 Students. If so, what you need is a LEFT JOIN coupled with COUNT and HAVING:
SELECT
a.AFirstName, a.ALastName
FROM dbo.Student s
LEFT JOIN dbo.Advisors a
ON a.AdvisorID = s.AdvisorID
GROUP BY
a.AdvisorID, a.AFirstName, a.ALastName
HAVING
COUNT(s.AdvisorID) <= 1
Notes:
Avoid using the old-style JOIN syntax.
Alias your tables to improve readability.
So I have three tables: companies, addresses and company_address.
For optimization reasons I need to copy city column from addresses table to companies table. Relation between companies and addresses is many to one (as many companies can occupy same address). They are connected through company_address table, consisting of address_id and company_id columns.
I found this solution for case without intermediate table: How to copy one column of a table into another table's column in PostgreSQL comparing same ID
Trying to modify query I came up with:
UPDATE company SET company.city=foo.city
FROM (
SELECT company_address.company_id, company_address.address_id, address.city
FROM address LEFT JOIN company_address
ON address.id=company_address.address_id
) foo
WHERE company.id=foo.company_id;
but it gives error:
ERROR: column "company" of relation "company" does not exist
I cant figure out what is going on. I'll be grateful for any ideas.
You don't need a subquery for that. Also, refer in the SET clause to your table columns without preceding with table name.
I believe that since your WHERE condition includes joined table, it should be INNER JOIN instead of a LEFT JOIN.
UPDATE company c
SET city = a.city
FROM address a
INNER JOIN company_address ca ON a.id = ca.address_id
WHERE c.id = ca.company_id
Note how using aliases for table names shortens the code and makes it readable at the very first glance.
You're right syntactically, you just don't need the table name at the beginning of the update statement:
UPDATE company SET city=foo.city
FROM (
SELECT company_address.company_id, company_address.address_id, address.city
FROM address LEFT JOIN company_address
ON address.id=company_address.address_id
) foo
WHERE company.id=foo.company_id;
I hope that i can explain it right, because i'm not a native english speaker.
I have 2 tables- Document and Vehicle.
As you can see in the SELECT query, the columns Id(Vehicle) and VehicleId(Document) are common.
The columns SeriePolita,NumarPolita,d.Status are from the Document table and v.CascoCategorieVehicul,v.NumarInmatriculare from the Vehicle table.
This code was written some time ago ( is working - no problem in the syntax ) by someone who left the company and my colleagues are using this code and weren't able to explain it to me.
My question is -
Can some explain or put a link with some info, for the Vehicle v on v.Id = d.VehicleId. What is the meanning of vehicle v and how is using d.Status different that Status, without the d.
select
SeriePolita,
NumarPolita,
d.Status,
v.CascoCategorieVehicul,
v.NumarInmatriculare
from Document d
inner join Vehicle v on v.Id = d.VehicleId
where EndDate>'2015-06-30' and d.Class='CASCO' and Status in ('Polita', 'Anulat')
Vehicle v is a reference to the Vehicle table by the alias v. The reason for doing this is so you don't have to type, for instance inner join Vehicle on Vehicle.Id = Document.VehicleId - it's shortened and more concise and you can refer to the alias in the select and where clause.
Now suppose that there is a Status column in both tables, without referring to it by either table name or alias, you would get an ambiguous column error as the DB engine would not know which table column you are referring to. If there is only only Status column then your query will run fine, although it is unclear which table the column actually belongs to!
See more on this in the documentation:
Using Table Aliases
the code you have can be rewritten as follows:
select
d.SeriePolita,
d.NumarPolita,
d.Status,
v.CascoCategorieVehicul,
v.NumarInmatriculare
from Document AS d
inner join Vehicle AS v on v.Id = d.VehicleId
where EndDate>'2015-06-30'
and d.Class='CASCO'
and Status in ('Polita', 'Anulat');
mind the AS keyword between Document and d: that is the keyword to tell the system that the object Document from now on will be 'known as' d.
it is a keyword that is not mandatory, may be omitted safely as in your code.
on msdn you can find details about table aliasing.
„Vehicle v” is a shorthand for „Vehicle as v”
Here the “v” is just an alias for the table “Vehicle” so you can just access columns of the table using this alias.
v.NumarInmatriculare has excactly same semantic meaning that Vehicle.NumarInmatriculare
Regards
Jan
Here Vehicle is a table and v is a table alias.
see this for more info https://technet.microsoft.com/en-us/library/ms187455%28v=sql.105%29.aspx
http://www.w3schools.com/sql/sql_alias.asp
Vehicle v on v.Id = d.VehicleId
v.Id means Id column from Vehicle table and d.VehicleId means VehicleId from document table
Can some explain or put a link with some info, for the Vehicle v on
v.Id = d.VehicleId. What is the meanning of vehicle v and how is using
d.Status different that Status, without the d.
TLDR: vehicle v means that v is an alias for vehicle and anywhere we use v.columnname would mean vehicle.columnname
Detail explanation:
Often in SQL queries we create an alias for table names, so that we can refer to them using that alias instead of original table name. There are multtiple reasons to do so. Let me introduce to two common ones.
When resolving ambiguity in column name Like when you want to join two tables say Employee and Spouse and both have same columns ID and name. And you want to select both names in your output. So query syntax is like
SELECT name,name FROM Employee JOIN Spouse on ID=ID
This query can not execute as both ID columns and name columns are in both tables and cannot be resolved to a single table correctly. Correct way to write query is
SELECT Employee.name,Spouse.name FROM Employee JOIN Spouse on Employee.ID=Spouse.ID
Note that though this query executes, still the output will be like
name | name
________________
John | Jane
Margaret | Dave
Still you cannot guess spouse or Employee from output. Good way is to use Alias like this
SELECT Employee.name as EmployeeName,Spouse.name as SpouseName FROM Employee JOIN Spouse on Employee.ID=Spouse.ID
which gives you output like
EmployeeName | SpouseName
___________________________
John | Jane
Margaret | Dave
This syntax columnname as aliasname allows us to use aliasname instead of original column name, clearly any ambiguity or renaming column so that expected output makes sense especially in case of join
Now if the example had both spouse and employees stored in same table say Person, so we had create self join
SELECT Person.name as EmployeeName,Person.name as SpouseName FROM person JOIN person on person.ID=person.ID
now again we have the same problem as both person.ID and Person.name repeat even though they are qualified by their table names, as there are two table names person and person (left and right side of JOIN).
In this case to resolve ambiguity we need to give alias to table names and refer to column using those alias like below:
SELECT Employee.name as EmployeeName,Spouse.name as SpouseName FROM person as Employee JOIN person as Spouse on Employee.ID=Spouse.ID
note that as keyword can be omitted. Most DBAs do not use it. So this below query is same as before.
SELECT Employee.name EmployeeName,Spouse.name SpouseName FROM person Employee JOIN person Spouse on Employee.ID=Spouse.ID
When you need to use a shorter, less typo prone name in query
Now wouldn't it be better that instead of writing long names again and again we can use shorter alias. Well we can. So both queries below produce same output.
This is same as
SELECT Employee.name as EmployeeName,Spouse.name as SpouseName FROM Employee JOIN Spouse on Employee.ID=Spouse.ID
this
SELECT E.name EmployeeName,S.name SpouseName FROM Employee E JOIN Spouse S on E.ID=S.ID
I am trying to use a second SELECT to get some ID, then use that ID in a second SELECT and I have no idea how.
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
This post got long, but here is a short "tip"
While the syntax of my select is bad, the logic is not. I need that "x" somehow. Thus the second select is the most important. Then I have to use that "x" within the first select. I just don't know how
/Tip
This is the only thing I could imagine, I'm very new at Sql, I think I need a book before practicing, but now that I've started I'd like to finish my small program.
EDIT:
Ok I looked up joins, still don't get it
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
LEFT JOIN Distribution ON
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
Get error msg at AS and Left
I use name to find ID from upper red, I use the ID I find FROM upper red in lower table. Then I match the ID I find with Green. I use Green ID to find corresponding Name
I have California as output data from C#. I want to use California to find the DistributionID. I use the DistributionID to find the EmployeeID. I use EmployeeID to find Name
My logic:
Parameter: Distribution.Name (from C#)
Find DistributionID that has Distribution.Name
Look in Employment WHERE given DistributionID
reveals Employees that I am looking for (BY ID)
Use that ID to find Name
return Name
Tables:
NOTE: In this example picture the Employee repeats because of the select, they are in fact singular
In "Locatie" (middle table) is Location, I get location (again) from C#, I use California as an example. I need to find the ID first and foremost!
Sory they are not in english, but here are the create tables:
Try this:
SELECT angajati.Nume
FROM angajati
JOIN angajari ON angajati.AngajatID = angajari.AngajatID
JOIN distribuire ON angajari.distribuireid = distribuire.distribuireid
WHERE distribuire.locatie = 'california'
As you have a table mapping employees to their distribution locations, you just need to join that one in the middle to create the mapping. You can use variables if you like for the WHERE clause so that you can call this as a stored procedure or whatever you need from the output of your C# code.
Try this solution:
DECLARE #pLocatie VARCHAR(40)='Alba'; -- p=parameter
SELECT a.AngajatID, a.Nume
FROM Angajati a
JOIN Angajari j ON a.AngajatID=j.AngajatID
JOIN Distribuire d ON j.DistribuireID=d.DistribuireID
WHERE d.Locatie=#pLocatie
You should add an unique key on Angajari table (Employment) thus:
ALTER TABLE Angajari
ADD CONSTRAINT IUN_Angajari_AngajatID_DistribuireID UNIQUE (AngajatUD, DistribuireID);
This will prevent duplicated (AngajatID, DistribuireID).
I don't know how you are connecting Emplyee(sic?) and Employment, but you want to use a join to connect two tables and in the join specify how the tables are related. Joins usually look best when they have aliases so you don't have to repeat the entire table name. The following query will get you all the information from both Employment and Distribution tables where the distribution location is equal to california. You can join employee to employment to get name as well.
SELECT *
FROM Employment e
JOIN Distribution d on d.DistributionID = e.DistributionID
WHERE d.Location = 'California'
This will return the contents of both tables. To select particular records use the alias.[Col_Name] separated by a comma in the select statement, like d.DistributionID to return the DistributionID from the Distribution Table