SQL query for Joining three tables with condition - sql

I have three tables: vehicle, addressrole, address. I have two tasks,
I have to find the number of records from address role table based on one of the date column in the vehicle table. I am able to get the expected resulted using inner join between vehicle and addresssrole table.
Based on the above results I have to find the records from addressrole table which are not available in the address table. This is the query I am using:
SELECT addressrole.*,
address.*
FROM addressrole
LEFT JOIN address ON addressrole.ID = address.FK_ADDRESSROLE_ID
INNER JOIN vehicle
ON vehicle.ID = addressrole.FK_EVN_ID
AND creationdate > '2018-03-01'
The above query results all the records from address and addressrole tables based on the inner join from vehicle and addressrole table.
Is there any way to get only the unmatched records from addressrole table based on the following inner join?

Is this what you want?
SELECT ar.*, a.*
FROM addressrole ar INNER JOIN
vehicle v
ON vehicle.ID = ar.FK_EVN_ID AND
?.creationdate > '2018-03-01' LEFT JOIN
address a
ON ar.ID = a.FK_ADDRESSROLE_ID
WHERE a.FK_ADDRESSROLE_ID IS NULL;

Related

Error in Count(),Group by and Joins in sql server

the result is very different when i separated statements...
SELECT Company.CompanyID,
Count(Projects2.CompanyID) as TotalProjects,
Count(Jobs2.CompanyID) as TotalJobs,
Count(Employees.CompanyID) as TotalEmployess
FROM Company JOIN
Projects2 ON Company.CompanyID = Projects2.CompanyID
JOIN Company Employees ON Company.CompanyID = Employees.CompanyID
JOIN Company Jobs2 ON Jobs2.CompanyID = Company.CompanyID
Group by Company.CompanyID
the result
why repeat the values?
any ideas?
I think you are counting the wrong ID. What is the primary key for the projects2, jobs2 and employees tables? Your query should be like this:
SELECT Company.CompanyID,
COUNT(DISTINCT Projects2.Primary_Key) as TotalProjects,
COUNT(DISTINCT Jobs2.Primary_Key) as TotalJobs,
COUNT(DISTINCT Employees.Primary_Key) as TotalEmployees
FROM Company
LEFT JOIN Projects2 ON Company.CompanyID = Projects2.CompanyID
LEFT JOIN Employees ON Company.CompanyID = Employees.CompanyID
LEFT JOIN Jobs2 ON Company.CompanyID = Jobs2.CompanyID
GROUP BY Company.CompanyID
Also, use LEFT joins instead of just JOIN and you don't need to keep repeating Company.
The reason you are getting the same count for each table is because of your joins. Say there are five records in the projects table with a given company ID but there are only three records in the jobs table with that ID. When you join company table to the project table you will have five records. Then when you join the jobs table, your joined table will have 15 (5*3) records for that company ID, because each job record will join to each of the five project records. Hence, you only want to select the distinct records from each table, using their primary key.
Your query is joining to the Company table 3 times (aliasing it as Employees and Jobs2 in the later instances). Just a typo?
You're getting the same values in each of the columns "TotalProjects", "TotalJobs", and "TotalEmployees", because your JOIN (in the future consider using INNER JOIN instead, as it does the same thing but is clearer) is producing results in which there are matching values across the joined tables - so you will end up with a like number of each result.

Request to existed table' value

There are two table
1st one(Empl):
Name
Id
2nd one (Phone):
Phone
id_emp
Each employe has got one two or three pnones or does not have anything
The task is to get all employes with phones
Am I correct ?
SELECT Empl.Name, Empl.id, phone.phone
FROM Empl
LEFT JOIN phone
ON empl.Id=phone.id_emp
where phone.phone >= 1
ORDER BY Empl.Name, Empl.Id;
If you need to get records that exists in both tables, you can use INNER JOIN. That will return only those employees that have at least one phone.
INNER JOIN returns rows from both sides that meet the join condition and only those rows. LEFT JOIN you used will return also those rows from Empl table that don't have any phones (don't match the join condition empl.Id=phone.id_emp).
SELECT Empl.Name, Empl.id, phone.phone
FROM Empl
INNER JOIN phone
ON empl.Id=phone.id_emp
ORDER BY Empl.Name, Empl.Id;

Why does this query produce the same information?

I am using the Lyric Database. I am wondering why this query produces the same information:
SELECT M.LastName, A.ArtistName
FROM members M
LEFT JOIN XRefArtistsMembers X USING(MemberID)
JOIN Artists A on X.ArtistID = A.ArtistID;
And
SELECT M.LastName, A.ArtistName
FROM members M
INNER JOIN XRefArtistsMembers X USING(MemberID)
JOIN Artists A on X.ArtistID = A.ArtistID;
There are no errors. I can post relevent records from the tables by request.
INNER JOIN only chooses only those entries from the two joined tables, where the values from the fields in the JOIN clause match in both tables.
LEFT JOIN chooses all entries from the first table, combined with either the matched entries from the second table or with NULL values instead, if there was no match.
If every row from the first table had a match in the second table on the field mentioned in the JOIN clause, then both INNER JOIN and LEFT JOIN will produce the same results.

I want to retrieve data from 4 tables in SQL Server

I want to retrieve data from 4 tables. Patient table has id as PK which is the foreign key in other three tables ett, phar and ssc. Where a patient lie in only one category. i.e patient id pt1 exists in either of the 3 tables. now I want to retrieve patient info along with its associated category.
My query is:
SELECT *
FROM Patient p
INNER JOIN ETT t
ON p.Patient_ID = t.Patient_ID || INNER JOIN Pharmacological ph
ON p.Patient_ID = ph.Patient_ID
I used OR clause because I want only 1 inner join executing at one time. but its not giving me results, any suggestions??
....Patient table has ID as PK which is the foreign key in other three
tables name: ett, phar and ssc where a patient lie in only one
category. Example, patient id pt1 exists in either of the 3 tables.
Based on your statement, you can join all the tables in table Patient using LEFT JOIN since a record can only exist on one table. The query below uses COALESCE which returns the first non-null value with int the list.
The only thing you need is to manually specify the column names that you want to be shown on the list as shown below.
SELECT a.*,
COALESCE(t.colA, p.ColA, s.ColA) ColA,
COALESCE(t.colB, p.ColB, s.ColB) ColB,
COALESCE(t.colN, p.ColN, s.ColN) ColN
FROM Patient a
LEFT JOIN ETT t
ON a.Patient_ID = t.Patient_ID
LEFT JOIN Phar p
ON a.Patient_ID = p.Patient_ID
LEFT JOIN SSC s
ON a.Patient_ID = s.Patient_ID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
For or - do not ise ||, use "or"
You cannot join with or, you need re-format your query.

Help with SQL INNER JOIN statement

I have 2 tables , one showing me customer addresses and one other table showing all the order data. i would like to query the two tables using a JOIN so that i can get a result set shwoing me all the email addresses for customers that have not ordered in the last year.
so far i have this , but my inner join is not working, if you may help:
SELECT SHH.CUST_NO,ADR.EMAIL
FROM SALES_HISTORY_HEADER SHH,ADDRESS ADR
INNER JOIN ADR ON
SHH.CUST_NO = ADR.CUST_NO
GROUP BY SHH.CUST_NO
HAVING Max(SHH.INVOICE_DATE) < '20100728'
You were mixing join styles. If you're going to use explicit joins (and you should) then you specify the second table on the JOIN rather than listing all the tables in the FROM clause.
SELECT SHH.CUST_NO,ADR.EMAIL
FROM SALES_HISTORY_HEADER SHH
INNER JOIN ADDRESS ADR
ON SHH.CUST_NO = ADR.CUST_NO
GROUP BY SHH.CUST_NO, ADR.EMAIL
HAVING Max(SHH.INVOICE_DATE) < '20100728'