Joining multiple tables and getting multiple attributes from one of them - sql

I'm trying to join multiple tables together for building a report. The report lists a course, revisions made to it, and who requested, made and approved the revisions.
Under requested, made an approved, the values are employee numbers. I'm trying to join my innerjoined table above, with the Employee table so I can list the names (not just employee numbers) of those that requested, made and approved revisions.
This is what I have which I know is totally wrong.
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] ON OPG_Employees.EmployeeID = CourseRevisions.UpdatedBy
AND OPG_Employees.EmployeeID = CourseRevisions.ApprovedBy
AND OPG_Employees.EmployeeID = CourseRevisions.RequestedBy
This only returns a single result which just happens to have the same employee ID listed for all 3 (Requested, Approved and Updated)
How would i get it so I can get the table result for individual employees in each?

You have to join to the OPG_Employees table once for each field, i.e. 3 times in the example above. One INNER JOIN to it for UpdatedBy, one INNER JOIN for ApprovedBy, one INNER JOIN for RequestedBy.
Something like so:
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] empUpdatedBy ON empUpdatedBy.EmployeeID = CourseRevisions.UpdatedBy
INNER JOIN [dbo].[OPG_Employees] empApprovedBy ON empApprovedBy.EmployeeID = CourseRevisions.ApprovedBy
INNER JOIN [dbo].[OPG_Employees] empRequestedBy ON empRequestedBy.EmployeeID = CourseRevisions.RequestedBy

You need a separate join for each employee being referenced:
SELECT *
FROM Courses INNER JOIN
CourseRevisions
ON CourseRevisions.PELID = Courses.PELID INNER JOIN
CourseGroups
ON CourseGroups.CourseGroupID = Courses.CourseGroupID INNER JOIN
[dbo].[OPG_Employees] UpdateEmp
ON UpdateEmp.EmployeeID = CourseRevisions.UpdatedBy INNER JOIN
[dbo].[OPG_Employees] ApprovedEmp
on OPG_ApprovedEmp.EmployeeID = CourseRevisions.ApprovedBy INNER JOIN
[dbo].[OPG_Employees] RequestedEmp
on RequestedEmp.EmployeeID = CourseRevisions.RequestedBy
Your original formulation required that all three ids be exactly the same.

Related

How to Query across multiple tables

I have the following DB. Is this a proper query to get the first name of the authors loaned by Members ? or is there a more efficient way ?
select M.MemberNo, A.FirstName
from Person.Members as M
INNER JOIN Booking.Loans as L
on M.MemberNo = L.MemberNo
INNER JOIN Article.Copies as C
on L.ISBN = C.ISBN and L.CopyNo = C.CopyNo
INNER JOIN Article.Items as I
on C.ISBN = I.ISBN
INNER JOIN Article.Titles as T
on I.TitleID = T.TitleID
INNER JOIN Article.TitleAuthors as TA
on T.TitleID = TA.TitleID
INNER JOIN Article.Authors as A
on TA.AuthorID = A.AuthorID
;
go
The most direct way to get the names of authors of books that are lent out to members would be like this:
Get member loans (Booking.Loans)
Get books for member loans (Article.Items)
Get authors for the books for member loans (Article.TitleAuthors)
Get first names for authors for the books for member loans (Article.Authors)
select L.MemberNo, A.FirstName
FROM Booking.Loans as L
INNER JOIN Article.Items as I
on L.ISBN = I.ISBN
INNER JOIN Article.TitleAuthors as TA
on I.TitleID = TA.TitleID
INNER JOIN Article.Authors as A
on TA.AuthorID = A.AuthorID
The advantage to joining all the tables like you have your original query is to ensure that each intermediary table was INSERTed into or DELETEd from correctly.
For example, your original query wouldn't return any rows for a member if they had a loan (i.e. a row in the Loans table) but for some reason didn't have an entry in the Members table. If you can assume that (or don't care if) there will always be a row in the Members table if there is a row in the Loans table, then you can exclude the join to Person.Members.
You can apply this same logic to the other intermediary tables (Member, Copies, Title) too, if needed. If you need a specific piece of data from one of the these tables, though, then you would need to include them in the joins.

why I get different number of records when using LEFT JOIN

I need to join many to many relationship. In order to do that I am using
LEFT OUTER JOIN
(SELECT
SICCode, QuoteID
FROM
NetRate_Quote_Insur_Quote_Busin
WHERE
QuoteID IN
(SELECT DISTINCT QuoteID
FROM NetRate_Quote_Insur_Quote_Busin)) nr ON nr.QuoteID = tblQuotes.QuoteID
The goal is to bring the column SICCode. For that, I am using DISTINCT statement to make relationship many to one. But for some reason I got different number of records.
The last LEFT OUTER JOIN is the one that I am stuck with
Full query looks like this:
SELECT
MONTH(INV.EffectiveDate) AS Effective_Month,
tblQuotes.PolicyNumber,
YEAR(INV.EffectiveDate) AS Effective_Year,
INV.EffectiveDate,
INV.[InvoiceDate] as [Billed Date],
tblQuotes.ExpirationDate as [Policy Expiration Date],
INV.DueDate,
dbo.tblFin_InvoiceDetails.AmtBilled AS Written
FROM
tblClientOffices
INNER JOIN
tblInsureds (NOLOCK)
INNER JOIN
tblFin_Invoices INV
INNER JOIN
tblFin_InvoiceDetails ON INV.InvoiceNum = dbo.tblFin_InvoiceDetails.InvoiceNum
INNER JOIN
tblCompanyLines (NOLOCK) ON dbo.tblFin_InvoiceDetails.CompanyLineGuid = dbo.tblCompanyLines.CompanyLineGUID
INNER JOIN
lstLines (NOLOCK) ON dbo.tblCompanyLines.LineGUID = dbo.lstLines.LineGUID
AND dbo.tblCompanyLines.LineGUID = dbo.lstLines.LineGUID
INNER JOIN
tblProducerLocations (NOLOCK) ON INV.ProducerLocationGUID = tblProducerLocations.ProducerLocationGUID
ON tblInsureds.InsuredGUID = INV.InsuredGUID
INNER JOIN
tblQuotes (NOLOCK) ON INV.QuoteID = tblQuotes.QuoteID
AND INV.QuoteControlNum = tblQuotes.ControlNo
INNER JOIN
lstPolicyTypes (NOLOCK) ON tblQuotes.PolicyTypeID = lstPolicyTypes.PolicyTypeID
INNER JOIN
tblSubmissionGroup (NOLOCK) ON tblQuotes.SubmissionGroupGuid = tblSubmissionGroup.SubmissionGroupGUID
INNER JOIN
tblCompanyLocations (NOLOCK) ON tblQuotes.CompanyLocationGuid = tblCompanyLocations.CompanyLocationGUID
INNER JOIN
tblUsers (NOLOCK) ON INV.UnderwriterUserGUID = tblUsers.UserGUID
ON tblClientOffices.OfficeGUID = tblQuotes.IssuingLocationGuid
LEFT OUTER JOIN
tblUsers tblUsers_1 (NOLOCK) ON tblSubmissionGroup.InHouseProducerUserGuid = tblUsers_1.UserGUID
LEFT OUTER JOIN
(SELECT SICCode, QuoteID
FROM NetRate_Quote_Insur_Quote_Busin
WHERE QuoteID IN
(SELECT DISTINCT QuoteID
FROM NetRate_Quote_Insur_Quote_Busin)) nr ON nr.QuoteID = tblQuotes.QuoteID
WHERE
(tblInsureds.TestInsured = 0)
AND (INV.Failed = 0)
AND (tblFin_InvoiceDetails.ChargeType = 'p')
ORDER BY
YEAR(INV.EffectiveDate),
MONTH(INV.EffectiveDate),
lstLines.LineID
What would be the trick to bring Column SICCode and keep the same number of records?
I also tried to do this:
LEFT OUTER JOIN NetRate_Quote_Insur_Quote_Busin nr ON nr.QuoteID =
(
SELECT distinct QuoteID from NetRate_Quote_Insur_Quote_Busin
where QuoteID =tblQuotes.QuoteID
)
But it gives me more records than it should be
Also tried this one:
LEFT OUTER JOIN
( SELECT TOP 1 SICCode, QuoteID
FROM NetRate_Quote_Insur_Quote_Busin
WHERE QuoteID IN
(SELECT DISTINCT QuoteID
FROM NetRate_Quote_Insur_Quote_Busin)order by QuoteID desc ) nr ON nr.QuoteID = tblQuotes.QuoteID
But doesnt bring me any SICCode
DISTINCT will not change a many to many relationship to a many to one. It looks like your data has many SICCode values to one QuoteID which is why you getting higher record counts.
You could use a window function like Rank, but this is an arbitrary selection.
You could try to pivot/unpivot the SICCode column, but that would give you 1 column for each possible value, which would not work if you have a large number of unique values in the column.
A final option is to create a new table which has a QuoteID column and a SICCodes column, then populate it withSICCodes being a comma seperated list of the codes.
In conclusion, there is no easy way to change a many to many relationship to a many to one relationship unless you are willing to lose data or there are only a few values.

Joining multiple tables to one table in sql

I have a rather complex (well for me) sql query happening and I am having trouble with some concepts.
I have the following sql on a webpage that i am building
SELECT
[dbo].[Enrolment].[_identity], [dbo].[Enrolment].CommencementDate,
[dbo].[Enrolment].CompletionDate, [dbo].[Enrolment].enrolmentDate,
[dbo].[Course].name coursename, [dbo].[Course].Identifier as QUALcode,
[dbo].[Person].givenName, [dbo].[Person].Surname,[dbo].[Employer].name as empname,
[dbo].[Employer].Address1,[dbo].[Employer].Suburb,[dbo].[Employer].Phone,
[dbo].[Employer].PostCode,[dbo].[EnrolmentStatus].name as enrolname,
[dbo].[Student].identifier,[dbo].[Student].person,[dbo].[Contact].person as CONTACTid
FROM
(((([dbo].[Enrolment]
LEFT JOIN
[dbo].[Course] ON [dbo].[Enrolment].course = [dbo].[Course].[_identity])
LEFT JOIN
[dbo].[Employer] ON [dbo].[Enrolment].employer = [dbo].[Employer].[_identity])
LEFT JOIN
[dbo].[EnrolmentStatus] ON [dbo].[Enrolment].status = [dbo].[EnrolmentStatus].[_identity])
LEFT JOIN
[dbo].[Student] ON [dbo].[Enrolment].student = [dbo].[Student].[_identity])
LEFT JOIN
[dbo].[Person] ON [dbo].[Student].person = [dbo].[Person].[_identity]
LEFT JOIN
[dbo].[Contact] ON [dbo].[Employer].[_identity] = [dbo].[Contact].employer
WHERE
(([dbo].[EnrolmentStatus].name) = 'training'
OR
([dbo].[EnrolmentStatus].name) = 'enrolled')
This is working fine but what I would like to do is join to the [dbo].[Person] table again but this time joining from another table so the code I effectively need to patch into the above statement is
LEFT JOIN
[dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])
LEFT JOIN
[dbo].[Person] ON [dbo].[Trainer].person = [dbo].[Person].[_identity]
I then need to be able to get from the person table the name of the student and the name of the trainer, so I need 2 records from the person table for every record from the Enrolment table, the fields I need from the person table are the same for both trainer and student in that I am trying to get the given name and surname for both.
Any help or pointers would be most appreciated.
You have to just use replace your from clause with this. You have to just first use the Trainer table join, then Person table, then use the AND keyword to use multiple mapping with single table
FROM (((([dbo].[Enrolment]
LEFT JOIN [dbo].[Course] ON [dbo].[Enrolment].course = [dbo].[Course].[_identity])
LEFT JOIN [dbo].[Employer] ON [dbo].[Enrolment].employer = [dbo].[Employer].[_identity])
LEFT JOIN [dbo].[EnrolmentStatus] ON [dbo].[Enrolment].status = [dbo].[EnrolmentStatus].[_identity])
LEFT JOIN [dbo].[Student] ON [dbo].[Enrolment].student = [dbo].[Student].[_identity])
LEFT JOIN [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])
LEFT JOIN [dbo].[Person] ON [dbo].[Student].person = [dbo].[Person].[_identity]
AND [dbo].[Trainer].person = [dbo].[Person].[_identity]
LEFT JOIN [dbo].[Contact] ON [dbo].[Employer].[_identity] = [dbo].[Contact].employer
Use aliasing like this..
LEFT JOIN [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])
LEFT JOIN [dbo].[Person] AS p ON [dbo].[Trainer].person = p.[_identity]
If I get your question right - what you are trying to do is to join the same table twice in your SQL. You have one table Person which has both student and trainer information and you want to see their details side by side in your result set. So you need to join Person once with Student and another time with Trainer
To do this - you will have to join Person table together. Give your tables an alias like the other answers have suggested. Then your FROM clause can look like this -
FROM (((([dbo].[Enrolment]
LEFT JOIN [dbo].[Course] ON [dbo].[Enrolment].course = [dbo].[Course].[_identity])
LEFT JOIN [dbo].[Employer] ON [dbo].[Enrolment].employer = [dbo].[Employer].[_identity])
LEFT JOIN [dbo].[EnrolmentStatus] ON [dbo].[Enrolment].status = [dbo].[EnrolmentStatus].[_identity])
LEFT JOIN [dbo].[Student] ON [dbo].[Enrolment].student = [dbo].[Student].[_identity])
LEFT JOIN [dbo].[Person] P1 ON [dbo].[Student].person = P1.[_identity]
LEFT JOIN [dbo].[Contact] ON [dbo].[Employer].[_identity] = [dbo].[Contact].employer
LEFT JOIN [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])
LEFT JOIN [dbo].[Person] P2 ON [dbo].[Trainer].person = P2.[_identity]
....
....
Here P1 and P2 are two aliases for [Person]

Complex SQL Code

I am having trouble with a SQL query that corresponds to multiple different tables. I have written the following code, but it seems to combine every single booking with every invoice rather than finding the specific invoice that matches a specific booking. Any help would be greatly appreciated.
$sql = "SELECT INVOICE.invoice_id,
g.guest_first_name,
g.guest_last_name,
g.booking_num_guests,
g.booking_num_nights,
CURRENCY.currency_name,
INVOICE.invoice_nightly_rate,
INVOICE.invoice_deposit_amount,
INVOICE.invoice_paid,
ACCOUNT.account_name
FROM INVOICE
INNER JOIN BOOKING
ON INVOICE.invoice_booking_id = BOOKING.booking_id
INNER JOIN CURRENCY
ON INVOICE.invoice_currency_id = CURRENCY.currency_id
INNER JOIN ACCOUNT
ON INVOICE.invoice_account_id = ACCOUNT.account_id
INNER JOIN (
SELECT GUEST.guest_first_name,
GUEST.guest_last_name,
BOOKING.booking_num_guests,
BOOKING.booking_num_nights
FROM BOOKING
INNER JOIN GUEST
ON BOOKING.booking_guest_id = GUEST.guest_id) g
ON INVOICE.invoice_booking_id = BOOKING.booking_id";
Since you already joined INVOICE and BOOKING table in the first inner join, the last inner join should be between BOOKING and GUEST table. Change your query to below
$sql = "SELECT
INVOICE.invoice_id,
GUEST.guest_first_name,
GUEST.guest_last_name,
BOOKING.booking_num_guests,
BOOKING.booking_num_nights,
CURRENCY.currency_name,
INVOICE.invoice_nightly_rate,
INVOICE.invoice_deposit_amount,
INVOICE.invoice_paid,
ACCOUNT.account_name
FROM INVOICE
INNER JOIN BOOKING
ON INVOICE.invoice_booking_id = BOOKING.booking_id
INNER JOIN CURRENCY
ON INVOICE.invoice_currency_id = CURRENCY.currency_id
INNER JOIN ACCOUNT
ON INVOICE.invoice_account_id = ACCOUNT.account_id
INNER JOIN GUEST
ON BOOKING.booking_guest_id = GUEST.guest_id";

Inner Join of three different Tables

I am working in Oracle APEX. I want to make a report from three different tables (Patient, History, Treatment) through INNER JOIN . Tables are as fallows.
PATIENT (Par_Id(Pk),Pat_Name,Pat_Gender)
HISTORY (His_Id(Pk),Pat_id(Fk),Treated_By)
and
Treatment (
Treat_Id,
His_id(Fk),Pat_id(Fk)
,Treat_Type
,Charges)
How I am going to display all the mentioned columns of the three Tables in
the report.
Thanks.
You should always specify the columns to return, especially as the tables contain identical column names
SELECT p.Par_Id, p.Pat_Name, p.Pat_Gender,
h.His_Id, h.Treated_By,
t.Treat_Id, t.Treat_Type, t.Charges
FROM Patient p
INNER JOIN History h
ON p.PAR_ID = h.PAT_ID
INNER JOIN Treatment t
ON h.HIS_ID = t.HIS_ID AND p.PAR_ID = h.PAT_ID
This should do the trick
SELECT * FROM Patient p
INNER JOIN History h
ON p.PAR_ID = h.PAT_ID
INNER JOIN Treatment t
ON h.HIS_ID = t.HIS_ID AND p.PAR_ID = h.PAT_ID
Try this
Select * from
PATIENT inner join HISTORY on par_id=HISTORY.Pat_id
inner join Treatment on par_id=Treatment.Pat_id
It is too simple in mysql
SELECT
*
FROM PATIENT as p
LEFT JOIN HISTORY as h ON h.Pat_id = p.Pat_Id
LEFT JOIN Treatment as t ON t.His_id = h.His_Id