Request to existed table' value - sql

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;

Related

SQL query for Joining three tables with condition

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;

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.

All parent table rows left jon child table directly

I have three tables: patron, patron_address, and patron_phone
for every patron/
Patron has 1-3 Patron_address rows
patron_address has 0-4
I want to display all the rows from the patron table and all the phone numbers of phone_type = '4'.
However, when I use the query below, I only get rows that have a phone{type of 4, not all the patron rows.
I tried to get Access 2007 query designer to do this, but something is off-kilter. Patron_address rows have an address_type. Only patron_address rows with address_type 1 have a child phone record.
So how do I get all the patron rows regardless of whether they have a patron_phone of phone_type 1?
SELECT
PATRON.patron_id, PATRON_PHONE.PHONE_TYPE,
PATRON_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM
(PATRON
INNER JOIN
PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID)
LEFT JOIN
PATRON_PHONE ON PATRON_ADDRESS.ADDRESS_ID = PATRON_PHONE.ADDRESS_ID
WHERE
(((PATRON_PHONE.PHONE_TYPE) = '4'))
ORDER BY
PATRON.patron_id;
If I add the criterion that the address type must equal 1, I get absolutely nothing back, even though this combination exists in the database. Isn't the behavior I want the point of a left outer join? Thanks.
You have an INNER JOIN from the Patron table to Patron_Addr. This means that there must be matching records in both tables (by PATRON_ID) to return a value.
SELECT PATRON.patron_id, PATRON_PHONE.PHONE_TYPE, PATRON_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM PATRON
INNER JOIN PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID
LEFT JOIN PATRON_PHONE ON PATRON_ADDRESS.ADDRESS_ID = PATRON_PHONE.ADDRESS_ID
WHERE PATRON_PHONE.PHONE_TYPE = 4
ORDER BY PATRON.patron_id
You have a LEFT JOIN from address to table so there doesn't need to be a matching phone number.
Since you are filtering for a Phone Type of 4, it will ONLY allow records that do have a phone record where the PHONE_TYPE = 4.
Is your Phone Type field a number or text? SQL Server will try to convert them back and forth but others may not and give an error or just not match - I don't remember how Access might handle this situation.
If you remove the PHONE TYPE criteria, your address criteria should work.
If you want to get all records of parton with address 1 but only phone numbers that are TYPE = 4, change the WHERE PHONE_TYPE=4 to part of the LEFT JOIN:
SELECT PATRON.patron_id, PATRON_PHONE.PHONE_TYPE, PATRON_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM PATRON
INNER JOIN PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID
LEFT JOIN PATRON_PHONE ON PATRON_ADDRESS.ADDRESS_ID = PATRON_PHONE.ADDRESS_ID AND PATRON_PHONE.PHONE_TYPE = 4
WHERE PATRON_PHONE.ADDRESS_TYPE = 1
ORDER BY PATRON.patron_id
Access SQL:
SELECT PATRON.patron_id, P_PHONE.PHONE_TYPE, P_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM PATRON
INNER JOIN PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID
LEFT JOIN [SELECT * FROM PATRON_PHONE WHERE PATRON_PHONE.PHONE_TYPE = 4 ]. AS P_PHONE ON PATRON_ADDRESS.ADDRESS_ID = P_PHONE.ADDRESS_ID
WHERE PATRON_PHONE.ADDRESS_TYPE = 1
ORDER BY PATRON.patron_id
Access has some goofy syntax for subqueries. You could create a separate query for the P_Phone subquery instead but the results would (should?) be the same.

Inner join 3 tables - show rows for ID that isn't found in other tables

Hello I have this Inner join of 3 tables
SELECT ADDRESS.NAME, ADDRESS.SURNAME, ADDRESS.PLACE,ADDRESS.TOTAL_COST_YEAR, ADDRESS.AREA, ISNULL(SUM(RENT.TO_PAY), 0) AS SumToPay, ISNULL(SUM(ORDER.AMOUNT), 0) AS SumPaid FROM ORDER
INNER JOIN ADDRESS
ON ADDRESS.IDadresa=ORDER.IDadresa
INNER JOIN RENT
on RENT.IDadresa = ORDER.IDadresa
GROUP BY ADDRESS.NAME, ADDRESS.SURNAME,ADDRESS.PLACE,ADDRESS.TOTAL_COST_YEAR,ADDRESS.AREA
It works well but sometimes values which are found under IDadresa in table ADDRESS doesn't containg any records in other tables but I still need the rows from ADDRESSES to be shown.
I think it is because there isn't any record for that ID in columns RENT and ORDER. May someone please help me improve this code ?
Thank you for your time.
Since you need all Address rows everytime just use right join between order and adress, left join between address and rent
The following should provide the expected results:
SELECT a.NAME, a.SURNAME, a.PLACE,a.TOTAL_COST_YEAR, a.AREA, ISNULL(SUM(RENT.TO_PAY), 0) AS SumToPay, ISNULL(SUM(ORDER.AMOUNT), 0) AS SumPaid FROM ORDER
LEFT OUTER JOIN ADDRESS a
ON a.IDadresa=ORDER.IDadresa
RIGHT OUTER JOIN RENT
ON RENT.IDadresa = ORDER.IDadresa
GROUP BY a.NAME, a.SURNAME,a.PLACE,a.TOTAL_COST_YEAR,a.AREA

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.