I have 2 tables, Employee, and Transaction
Transaction
tID
cID
carID
eID
tDate
PickupDate
ReturnDate
Amount_Due
Employee
eID
fName
lName
Job
Manager
Hired
I need to calculate the commission (2.5%) and display that along with fName and lName.
I've calculated the commission, I think I've done the join correctly but can't quite figure out how to add in a SELECT to show the other two columns.
SELECT t.Amount_Due
, SUM(t.Amount_Due*0.025) AS Commission
FROM [Transaction] t
JOIN Employee e ON t.eID = e.eID
GROUP BY t.Amount_Due
You are grouping by the wrong columns, and you are trying to select Amount_Due and aggregate it at the same time:
SELECT e.fName
, e.lName,
, SUM(t.Amount_Due * 0.025) AS Commission
FROM [Transaction] t
JOIN Employee e
ON t.eID = e.eID
GROUP BY e.ID, e.fName, e.lName;
Probably just a typo - the JOIN should probably be ON t.eID = e.eId. You need to include both tables (table aliases) involved - you're currently just using the t.eID twice.
Try this :
SELECT
t.Amount_Due, SUM(t.Amount_Due*0.025) AS Commission
FROM
[Transaction] t
JOIN
Employee e ON t.eID = e.eID
GROUP BY
t.Amount_Due
As pointed out by #charlieface - it seems very odd that you're summing the Amount_due, and at the same time trying to group by that same column..... my best guess is that you probably want to group by the employee, based on their eID - so use something like this:
SELECT
e.eID, e.fName, e.lName, SUM(t.Amount_Due*0.025) AS Commission
FROM
[Transaction] t
JOIN
Employee e ON t.eID = e.eID
GROUP BY
e.eID, e.fName, e.lName
for that.
Related
I have data of two tables. In the first table I have employee id's and their names. In the second table, I have employee details (such as salary, location, join date, projects handled, designation).
I am trying to write a query where I can bring the data of maximum and minimum salary group by location and designation. This is what I have so far:
SELECT E.EMPID,I.EMPID, E.EMPNAME, I.EMPNAME,
B.[MAXIMUM SALARY],F.[MINIMUM SALARY],
B.DESIG,F.DESIG,B.LOCATION,F.LOCATION
FROM
((SELECT MAX (SALARY) [MAXIMUM SALARY], LOCATION, DESIG
FROM
EMPDETAILS
GROUP BY LOCATION, DESIG) B
JOIN
(SELECT C.EMPID,C.SALARY,C.GENDER,C.DESIG,
C.JOINON,C.LOCATION,C.PROJECTS,D.EMPNAME
FROM EMPDETAILS C
JOIN
EMPLOYEE D
ON C.EMPID=D.EMPID) E
ON E.DESIG=B.DESIG AND E.SALARY=B.[MAXIMUM SALARY] AND E.LOCATION =
B.LOCATION)
FULL OUTER JOIN[enter image description here][1]
((SELECT MIN(SALARY) AS [MINIMUM SALARY], LOCATION, DESIG
FROM EMPDETAILS
GROUP BY LOCATION, DESIG) F
JOIN
(SELECT G.EMPID,H.EMPNAME,G.DESIG,G.GENDER,
G.JOINON,G.LOCATION,G.PROJECTS,G.SALARY
FROM EMPDETAILS G
JOIN EMPLOYEE H
ON G.EMPID=H.EMPID) I
ON I.DESIG=F.DESIG AND F.[MINIMUM SALARY]=I.SALARY AND
I.LOCATION=F.LOCATION)
ON I.DESIG=E.DESIG
ORDER BY B.LOCATION, f.location
Giving me:
no clue what you really want/need, but if you want employeeID-level results, you'll need something like:
SELECT
d.*,
min_salary,
max_salary
FROM
EMPDETAILS d JOIN
EMPLOYEE e ON
d.EMPID=e.EMPID JOIN
(SELECT
empid,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM
EMPDETAILS
GROUP BY
empid) m ON
e.empid = m.empid
if you want min/max salaries grouped by LOCATION, DESIG as in your code, you'll need to return those results separately from employee-level details:
SELECT
LOCATION, DESIG,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM
EMPDETAILS
GROUP BY
LOCATION, DESIG
you don't need separate subqueries for your min and max groupings, you don't need to join your detail tables multiple times, and you don't need a full outer join.
I'm going to learn SQL, and having troubes with one of the excercises. "Get the number of customers each employee is responsible for" (I hope the translation is OK). I figured out following:
SELECT emp.EmployeeId, COUNT(cus.SupportRepId) AS Customers
FROM Employee AS emp JOIN Customer AS cus
ON cus.SupportRepId = emp.EmployeeId
GROUP BY emp.EmployeeId
The result is correct so far:
EmployeeId Customers
-------------------------
3 21
4 20
... ...
Now I thought it would be nice to addional employee's data in the result too (e.g. JobTitle from the table 'Employee'), but this doesn't seems to work:
SELECT emp.EmployeeId, emp.JobTitle, COUNT(cus.SupportRepId) AS Customers
FROM Employee AS emp JOIN Customer AS cus
ON cus.SupportRepId = emp.EmployeeId
GROUP BY emp.EmployeeId
I don't understand why. What should I have to in order to get the expected result:
EmployeeId JobTitle Customers
------------------------------------------------
3 Key Account Manager 21
4 Business Area Manager 20
... ...
I hope you can help. Have much thanks in before.
You can throw it in the GROUP BY:
SELECT e.EmployeeId, e.JobTitle, COUNT(c.SupportRepId) AS Customers
FROM Employee e JOIN
Customer c
ON c.SupportRepId = e.EmployeeId
GROUP BY e.EmployeeId, e.JobTitle;
Alternatively, you can use a correlated subquery or aggregate before joining:
SELECT e.*, c.Customers
FROM Employee e JOIN
(SELECT c.SupportRepId, COUNT(c.SupportRepId) AS Customers
FROM Customer c
GROUP BY c.SupportRepId
) c
ON c.SupportRepId = e.EmployeeId;
I have an EmployeeDepartmetn juction table like this. I have all the departments in Depeartment table and employees in Employee table..
I want to get departments for an particular employee along with the all the departments available in depeartment table.
It should be like Select DepartmentId, DepartmentName, EmployeeID from Query.
Main criteria here is, Need to display NULL if the employee dont have that department. I am confused here...please help.
Please give Linq Query
Thanks in Advance
Put criteria in your left join:
Select distinct a.DeptID, b.DepartmentName, b.EmployeeID
From Department a
left join EmployeeDepartment b
on a.DeptID = b.DeptID and b.EmployeeID = 1 --insert employee ID here
It will show all departments (even those with no employees), then show the employee ID you chose in the third column only if that employee is assigned there.
You can do this with conditional aggregation:
select DeptId,
max(case when EmployeeId = 1 then EmployeeId end) as EmployeeId
from EmployeeDepartment ed
group by DeptId;
EDIT:
If you have a departments table as well:
select d.deptid, d.name, ed.employeeid
from Departments d left join
EmployeeDepartment ed
on d.deptid = ed.deptid and
ed.employeeid = 1;
I have made the query below=>
SELECT FirstName,
LastName,
COUNT(*) AantalBestellingen,
YEAR(Orders.OrderDate) as Jaar
from Employees
RIGHT JOIN Orders ON (Employees.EmployeeID=Orders.EmployeeID)
WHERE Employees.ReportsTo IS NOT NULL
GROUP BY Employees.FirstName, Employees.LastName, YEAR (Orders.OrderDate)
ORDER BY YEAR (Orders.OrderDate)
I need to select the first name, lastname from the employees and the total orders they have processed within the last year.
After adding the group by year to the query it's not showing the unique employees anymore but it shows the years. I need to get only the unique employees and the last order year they have processed an order in.
Any advice what i'm doing wrong?
One way to do this is with an inner select:
SELECT
FirstName,
LastName,
COUNT(*) as AantalBestellingen,
(select YEAR(max(Orders.OrderDate)) from Orders O
where Employees.EmployeeID=O.EmployeeID) as Jaar
from
Employees
RIGHT JOIN Orders
ON (Employees.EmployeeID=Orders.EmployeeID)
WHERE
Employees.ReportsTo IS NOT NULL
GROUP BY
Employees.FirstName,
Employees.LastName
ORDER BY Jaar
You can place the Last Year condition in WHERE clause as follows
SELECT FirstName,
LastName,
COUNT(*) AantalBestellingen,
from Employees
RIGHT JOIN Orders ON (Employees.EmployeeID=Orders.EmployeeID)
WHERE Employees.ReportsTo IS NOT NULL AND YEAR(Orders.OrderDate) = 2014
GROUP BY Employees.FirstName, Employees.LastName
Can you try this one... This will give all employee, his only last year order count and last year.
;with cteMaxYear as
(select max(YEAR(Orders.OrderDate)) MaxOrderYear, EmployeeID
from Orders
group by EmployeeID)
SELECT FirstName,
LastName,
COUNT(*) AantalBestellingen,
m.MaxOrderYear as Jaar
from Employees e
left outer join cteMaxYear m
inner join Orders o ON m.EmployeeID=o.EmployeeID and m.MaxOrderYear=o.YEAR(Orders.OrderDate)
on e.EmployeeID = m.EmployeeID
WHERE e.ReportsTo IS NOT NULL
GROUP BY e.FirstName, e.LastName, m.MaxOrderYear
ORDER BY m.MaxOrderYear
The following script should return the name of the departments and the number of employees that are in those departments, the Marketing,Executive and Sales departments have '0' employees but instead of '0' , the returned value is '1'. How can I correct it?
select Department, Departments.DepartmentID, count(*) as 'NumOfEmps'
from Departments
left join Employees
on Employees.DepartmentID = Departments.DepartmentID
group by Departments.DepartmentID,Department
You can't do that all in one query. You need a sub-query to get the employee counts first, then get the related department information (name, etc.) using the aggregated results:
SELECT Department, Departments.DepartmentID, t.NumOfEmps
FROM Departments
LEFT JOIN (SELECT DepartmentID, count(*) as 'NumOfEmps'
FROM Employees
GROUP BY DepartmentID) t
ON t.DepartmentID = Departments.DepartmentID
I'm making some assumptions about your schema since it's not listed. Column names may be off a bit, but this is the general idea. Hope it helps.
Don't use Count(*) count the thing you want to count namely the employees.
Count(*) counts the whole row. Since there's always going to be at least one record for each Department in Departments when you do count(*) you'll always get at least 1
SELECT d.Department, d.DepartmentID, count(e.EmployeeID)
FROM Departments d
LEFT JOIN employees e
ON d.DepartmentID = e.DepartmentID
GROUP BY
d.Department, d.DepartmentID
DEMO