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

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.

Related

Joining tables and sum by attribute

I need to know how many employees does each company have for each country they are present in? I have to join two tables (companies and cities) and sum number of employees for each country.
SELECT *, SUM(EMPLOYEES)
FROM COMPANIES WHERE
JOIN CITIES
ON COMPANIES.CITYNAME = CITIES.CITYNAME
doesn´t work...
Tables to join and sum employees for each country
A couple of tips,
Try to alias your joins so your not typing the full table names in your join statements
Add a group by on every field your not aggregating on for your sum to work
SELECT CI.COUNTRYNAME,CO.CITYNAME,CO.COMPANYNAME,CI.POPULATION,CI.COUNTRYNAME,
SUM(EMPLOYEES) AS TOTAL_EMPLOYEES
FROM COMPANIES AS CO
JOIN CITIES AS CI
ON CO.CITYNAME = CI.CITYNAME
GROUP BY CI.COUNTRYNAME,CO.COMPANYNAME,CO.CITYNAME,CI.POPULATION
https://rextester.com/DLZNT87647

Join Query for 3 tables

I need to run a query which joins 3 tables and displays the unique set. I've 3 tables with the row below details:
Users : FName, LName, MobileNumber, STBNumber, CustID.
UserPay : STBNumber, CustID, PreviousDue, CurrentDue, ExpiryMonth,
ExpiryYear.
Calendar: ExpiryMonth, ExpiryMonthName.
Now, the problem with the query is that it is displaying the columns STBNumber, CustID, Month and year twice.
SELECT *
FROM users
INNER JOIN userpay
ON users.stbnumber = userpay.stbnumber
INNER JOIN calendar
ON userpay.expirymonth = calendar.month;
The query should display all the unique columns across all the tables.
So, I was working on a wrong join. Below is the query that solved my problem:
Select Users.FName, Users.LName, Users.MobileNumber, Users.STBNumber, Users.CustID, UserPay.PreviousDue, UserPay.CurrentDue, Calendar.ExpiryMonthName, UserPay.ExpiryYear from UserPay
INNER JOIN Users ON Users.STBNumber=UserPay.STBNumber
INNER JOIN Calendar ON UserPay.ExpiryMonth=Calendar.ExpiryMonth

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;

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;

Three tables join given me the all combination of records

When i written the query like the following.. It's written the combination of all the records.
What's the mistake in the query?
SELECT ven.vendor_code, add.address1
FROM vendor ven INNER JOIN employee emp
ON ven.emp_fk = emp.id
INNER JOIN address add
ON add.emp_name = emp.emp_name;
Using inner join, you've to put all the links (relations) between two tables in the ON clause.
Assuming the relations are good, you may test the following queries to see if they really make the combination of all records:
SELECT count(*)
from vendor ven
inner join employee emp on ven.emp_fk = emp.id
inner join address add on add.emp_name = emp.emp_name;
SELECT count(*)
add.address1
from vendor ven, employee emp, address add
If both queries return the same result (which I doubt), you really have what you say.
If not, as I assume, maybe you are missing a relation or a restriction to filter the number of results.