I'm not sure how to word the title correctly, so I apologize if it's confusing.
What I need to do is to count how many projects are run by each department, I need to display each department name and assign a name to the colum with computed results
DEPARTMENT Table Imgur
PROJECT Table Imgur
This is what I have tried so far, but it returns an error:
SELECT DISTINCT DepartmentName, COUNT(*) AS AmountOfProjects
FROM DEPARTMENT, PROJECT;
SELECT DepartmentName, Count(1) AS AmountOfProjects
FROM Project
GROUP BY DepartmentName
You don't need the Department table since the Full Name of the Department is in the projects table.
You left out the Group By. Whenever you have an aggregate function in your select, you need a Group By
When I tried user3083310 solution something still looked wrong, all the Departments were showing 5 projects, and that is wrong. I added the following join and it worked:
SELECT DEPARTMENT.DepartmentName, Count(*) AS AmountOfProjects
FROM PROJECT, DEPARTMENT
WHERE PROJECT.Department = DEPARTMENT.DepartmentName
GROUP BY DEPARTMENT.DepartmentName;
And the output is the following that matches the information the project table.
Department Name: Account Finance Marketing
AmountOfProjects: 1 - 2 - 2
Related
I have two different tables with records I need to join together in a way I can't quite figure out how to make work. My data looks like this.
Table A
Columns: Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager ... many other columns
Sample Values:
12345 Department1 Team1 67890 Yes/No
.
.
.
One employee per row, several thousand rows comprising the entire company
Table B
Employee_ID, Manager_ID ... other columns
The exact same data set as Table A
Currently I'm combining those two tables (and three others) with a simple join on Employee_ID, which I'm then using as a data source in Tableau to visualize the data.
What I'd like to do with a SQL script is as follows:
Check to see whether an employee in Table A is a manager or not based on the Is_a_Manager column
If they are, find an employee in Table B who is one of their direct reports by matching the employee ID in Table A to the Manager ID in Table B.
Lookup that direct report's department and team in Table A by matching the Employee_ID in Table B to Employee_ID in Table A and displaying the Employee_Department and Employee_Team columns.
Add the direct report's department and team to two new columns in the original manager's Table A row
I'd like the final output in Table A to be something like
Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager? ... Direct_Report_Department, Direct_Report_Team
Also, an important point is that some managers will have employees who are on different teams, so values in the Direct_Report_Department and Direct_Report_Team are not distinct. I only actually need any one employee's Department and Team to display, it doesn't matter which employee's it is.
Finally, I am able to do step 1 fairly easily in Tableau, so if the SQL script could do steps 2-4 and simply return a null value if the employee was not a manager, that would work for me as well.
Any ideas on how to accomplish this would be greatly appreciated. Thank you!
This should work based on the requirement provided. You don’t have to do any of the steps in Tableau and can simply export the output from the SQL as your data source
Select Tb1.Employee_ID, Tb1.Employee_Department, Tb1.Employee_Team, Tb1.Manager_ID, Tb1.Is_a_Manager, Tb3. Direct_Report_Department, Tb3. Direct_Report_Team
from Table_A Tb1
join (Select Manager_id, max(Employee_id) as emp_id from Table_B group by Manager_id) Tb2
on Tb1.Employee_id = Tb2.Manager_id
left join (Select Employee_ID, Employee_Department as Direct_Report_Department, Employee_Team as Direct_Report_Team from Table_A group by Employee_ID, Employee_Department, Employee_Team) Tb3
on Tb2.emp_id = Tb3.Employee_ID
where Tb1.Is_a_Manager = 'Yes';
I have 5 columns in a table
importcode
dateoftermination
hiredate
companyid
employeeid
each employee working in different companies.
For each employee id working in different company id. I need to get the import code of the record which has dateoftermination as null and hiredate as the latest to be compared with other companyid of the employee which has same import code to be displayed and others to be ignored.
The above process needs to be done for each employee id. Can you please provide sql query for this.
This is the query i have used
select eeceeid,
max(eecDateOfLastHire) as maxdate,
eecCOID
from EmpDetails
where eecDateOfTermination is null
group by eeceeid,eecCOID
but I am not getting the correct result.
It is Microsoft sql server
You need to use group by either with only companyid or with import code as you want same import code but max hires among all companies
select eeceeid,
max(eecDateOfLastHire) as maxDate
from EmpDetails
where eecDateOfTermination is null
group by eeceeid
Apologies for my simple problem, I am an absolute novice. I have the following code in separate queries
I am attempting to display 3 columns, the average male salary for a set job, average female salary for a set job and the JobID. Separately these queries work however I cannot work out how to combine them.
I have tried multiple solutions from this site for example trying to put multiple select statements inside
and also by using a 'union' solution however cannot get either to work.union This simply compiles them into a single column and sorts via salary not JobID.
SELECT Round(Avg(Salary)) AS AverageMaleSalary, JobID
FROM Employee WHERE Gender = "M"
GROUP BY JobID;
SELECT Round(Avg(Salary)) AS AverageFemaleSalary, JobID
FROM Employee WHERE Gender = "F"
GROUP BY JobID;
You could use conditional aggregation
SELECT JobId,ROUND(AVG(IIF(Gender='F', Salary, NULL))) AS AverageFemaleSalary
,ROUND(AVG(IIF(Gender='M', Salary, NULL))) AS AverageMaleSalary
FROM Employee
GROUP BY JobId;
I'm a beginner Programming student and nothing in my text book is helping me with this problem! I need to display these field names ProjectID, ProjectName, DepartmentPhone, EmployeeNumber, LastName, FirstName, EmployeePhone and also the field name Department.
The last field name is included in two separate tables so I used the WHERE clause to make it display.
However, I have to specifically display employees working on projects assigned by the 'Marketing' Department.
I can't get this to work... If I remove my current WHERE clause than Department will not work or display... But I think I need another Where clause to make it only display projects by the Marketing Department.
Here is my current code:
SELECT ProjectID, ProjectName, DepartmentPhone, EmployeeNumber, LastName, FirstName, EmployeePhone
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE PROJECT.Department = EMPLOYEE.Department
AND ProjectID IN
(SELECT PROJECT ID
FROM PROJECT
WHERE Department = 'Marketing')
ORDER BY ProjectID
I'm running it in Access as an SQL Query and the error that pops up says
Syntax error in query expression 'PROJECT.Department = EMPLOYEE.Department AND SELECT ProjectID, ProjectName, DepartmentPhone, EmployeeNumber, LastName, FirstName, EmployeePhone
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE PROJECT.Department = EMPLOYEE.Department
AND ProjectID IN
(SELECT PROJECT ID
FROM PROJECT
WHERE Department = 'Marketing')
ORDER BY ProjectID "
Without "AND" and everything past that it works just fine but displays all projects and not just those run by the Marketing Department. Please help!
When you use IN with select IN (SELECT you have to use exact one column, but I can see 2. Obviously you mean IN (SELECT ID FROM PROJECT...
P.S and yes,- you miss some joining...
My task is to find the number of occurrences of late timesheet submissions for each employee in our database. There are two tables which I have primarily been looking at, but I'm having trouble putting the two together and coming up with a decent view of the COUNT of occurrences and the employee ID for which they are associated with.
I have created this query which provides me with the EmployeeID for each occurrence.
SELECT db.Employee.EmployeeID
FROM db.LateTimesheets
INNER JOIN db.Employee ON Employee.LastName = LateTimesheets.LastName AND Employee.FirstName = Late Timesheets.FirstName
Now, with this simple query I have a view of the EmployeeID repeated however many times these incidents have occured. However, what I ultimately want to end up with is a table that displays a count for each occurance, along with the EmployeeID for which this count is associated with.
I would assume I would need to use the COUNT() function to count the amount of rows for each EmployeeID, and then select that value along with EmployeeID. However, I am having trouble structuring the subquery correctly, and everything I have tried thus far has only generated errors with MS SQL Server Management Studio.
A simpler version of usr's answer would be the following which avoids the construction of the derived table:
Select db.Employee.EmployeeID, Count( db.LateTimesheets.somecolumn ) As Total
From db.Employee
Left Join db.LateTimesheets
On LateTimesheets.LastName = Employee.LastName
And Late Timesheets.FirstName = Employee.FirstName
Group By db.Employee.EmployeeID
I may have misunderstood the question, but wouldn't GROUP BY solve your problem?
SELECT COUNT(db.LateTimesheets.somecolumn), db.Employee.EmployeeID
FROM db.LateTimesheets
INNER JOIN db.Employee ON Employee.LastName = LateTimesheets.LastName
AND Employee.FirstName = Late Timesheets.FirstName
GROUP BY db.Employee.EmployeeID
Just replace somecolumn with the name of a column that's actually in the table.
select e.*, isnull(lt.Count, 0) as Count
from Employee e
left join (
select LastName, count(*) as Count from LateTimesheets
) LateTimesheets lt on e.LastName = lt.LastName
The trick is to do the grouping in a derived table. You don't want to group everything, just the LateTimesheets.
We need a left join to still get employees with no LateTimesheets.