In sql server I have an Employee table and an Address table.
An Employee can have many Addresses.
I want to get the first 10 Employees with their Addresses.
SELECT *
FROM
Employee e
LEFT JOIN Address a ON a.EmployeeID = e.Id
WHERE
a.Street LIKE '%a%'
AND e.Name LIKE '%bob%'
ORDER BY e.Id
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
If each employee has 2 addresses associated with it I will get 10 rows but only 5 employees.
How do I get the 10 employees I want?
You can use dense_rank():
SELECT ea.*
FROM (SELECT e.*, a.*, -- should select the columns you really need
DENSE_RANK() OVER (ORDER BY e.id) as seqnum
FROM Employee e JOIN
Address a
ON a.EmployeeID = e.Id
WHERE a.Street LIKE '%a%' AND e.Name LIKE '%bob%'
) ea
WHERE seqnum <= 10
ORDER BY e.Id ;
Related
I have 4 tables in my db: incoming letters, outgoing letters, local letters and employee. In "letters" tables, there is a register_employee that have register this letter.
Every employee has his/her unique id (for example "3de9a23e-b927-4a27-a66a-c1f30b8c1464").
Tables look like this:
employee:
id first_name last_name age
incoming:
id register_employee summary letter adress date_sent
"Outgoing" and "local" tables look same as "incoming" table.
I need to count how many letters have been registered by each employee and which type.
Example results:
employee incoming outgoing local
Bob Marley 45 33 5
John Travolta 31 10 9
George Bush 98 15 38
I tried to use full joins and nested Select statments, but results that I have got are probably wrong cause I am getting millions of count for each employee (I don't have so many letters in db).
You can use OUTER JOIN and GROUP BY as follows:
-- Updated
You can use multiple sub-query:
select e.id, e.firstname,
(select count(*) from incoming i where i.register_employee = e.id) as incoming_letter,
(select count(*) from outgoing o where o.register_employee = e.id) as outgoing_letter,
(select count(*) from local l where l.register_employee = e.id) as local_letter
from employee e
We count the number of letters under different categories & left join them to the master employee table
select employee.first_name , t1.incoming, t2.outgoing, t3.local from (
select * from employee
left join
(select register_employee, count(*) as incoming from incoming_letters group by register_employee) t1 on employee.id = t1.register_employee
left join
(select register_employee, count(*) as outgoing from outgoing_letters group by register_employee) t2 on employee.id = t2.register_employee
left join
(select register_employee, count(*) as local from local group by register_employee) t3 on employee.id = t3.register_employee ) v
SELECT incom.id,incom.name,incom.incoming,out.outgoings,loc.local
FROM (SELECT
employees.id as id,
employees.short_name as name,
COUNT(incomings.register_employee) as incoming
FROM employees
LEFT JOIN incomings ON incomings.register_employee = employees.id)
GROUP BY employees.id, employees.short_name) as incom
LEFT JOIN (SELECT
employees.id as id,
COUNT(outgoings.register_employee) as outgoings
FROM employees
LEFT JOIN outgoings ON outgoings.register_employee = employees.id
GROUP BY employees.id) as out
ON incom.id=out.id
LEFT JOIN (SELECT
employees.id as id,
COUNT(local.register_employee) as local
FROM employees
LEFT JOIN outgoings ON local.register_employee = employees.id
GROUP BY employees.id) as loc
ON incom.id=loc.id;
What I need:
I am looking for a solution that can give me all the Employee Id's that have the same EmailAddress Column (the filter needs to be by EmailAddress).
I want to know what are the Id's correspondent to the duplicated Email Addresses and retrieve that information.
Table Employee:
Id | PlNumber | EmailAddress | EmployeeBeginingDate | EmployedEndDate | Name UserId(FK) | CreatedBy | CreatedOn
SELECT a.Id,a.EmailAddress
FROM Employee a
INNER JOIN (SELECT
Employee.Id as EmployeeId,
Employee.EmailAddress as EmailAddress,
FROM Employee
GROUP BY Employee.Id,Employee.EmailAddress
HAVING count(Employee.EmailAddress) > 1
) b
ON a.Id= b.EmployeeId
ORDER BY a.Id
I am always getting an error:
the multi-part identifier could not be bound.
I know why the error is happening but I couldn't solve this.
UPDATE: After a few changes the query is returning 0 rows but I know it should return at least 3 rows that I have duplicate values.
Try the below query as you have an aliased table Employee as a. So in place of Employee, you have to use a.
SELECT a.Id, a.EmailAddress
FROM Employee a
INNER JOIN (SELECT
Employee.EmailAddress as EmailAddress
FROM Employee
GROUP BY Employee.EmailAddress
HAVING count(Employee.EmailAddress) > 1
) b
ON a.EmailAddress = b.EmailAddress
ORDER BY a.Id
Live db<>fiddle demo.
Assuming the ids are different on each row, I would go for exists:
SELECT e.Id, e.EmailAddress
FROM Employee e
WHERE EXISTS (SELECT 1
FROM Employee e2
WHERE e2.EmailAddress = e.EmailAddress AND
e2.Id <> e.Id
)
ORDER BY e.EmailAddress;
Or, if you want to know the number of matches, use window functions:
SELECT e.Id, e.EmailAddress, cnt
FROM (SELECT e.*, COUNT(*) OVER (PARTITION BY e.EmailAddress) as cnt
FROM Employee e
) e
WHERE cnt >= 2;
I wrote a query which gives me top 3 salaries for the specific department id. For example for DepartmentId=100:
select distinct top 3 e.Id, e.Salary, dep.Id
from Employee e
inner join Department dep on e.DepartmentId = dep.Id and dep.Id = 100
and it's working fine.
Now, I want to run previous query for every department id and union all results. Something like this (written in a pseudocode):
Result <- empty
foreach depId in [Department].id
Result = Result UNION run previous query with depId (insead of 100)
How can I achive this with SQL?
If you only need the department's id in the results (and not its name also) then the join is not necessary because this id exists in the table Employee.
Use row_number() window function:
select e.Id, e.Salary, e.DepartmentId
from (
select
Id, Salary, DepartmentId,
row_number() over (partition by DepartmentId order by Salary desc) rn
from Employee
) e
where e.rn <= 3
You can use APPLY
select t.Id, t.Salary, t.DepartmentId
from Department dep
cross apply (
select distinct top 3 e.Id, e.Salary, e.DepartmentId
from Employee e
where e.DepartmentId = dep.Id) t
This will only return Employee rows for which parent Department exists.
I have 3 tables: Employee, Department and employeeProject.
The relation between employee and employeeproject is one-to-many. The relation between employee and department is many-to-one.
I want to write a query to select 10 employees who have worked in projects 3 and 4. The query should return employees of different departments if possible.
The query below kind of works. The only problem is that the relationship between employee and employeeproject is one-to-many, so it might return the same employee number multiple times.
I cannot use distinct because all fields in the order by clause should be used in select when using distinct.
select top 10 empid from employee e
inner join department d on d.depId=e.depid
inner join employeeProject p on p.empid=e.empid
where p.projectID in (3,4)
order by row_number() over(partition by e.depId order by e.empid)
Bit of a guess, but use an EXISTS?
SELECT TOP 10 e.empid
FROM employee e
JOIN department d ON e.depid = d.depid
WHERE EXISTS (SELECT 1
FROM employeeproject p
WHERE p.emdid = e.empid
AND p.projectid IN (3,4))
ORDER BY e.depid, e.empid;
I suggest aggregating by employee, and then using an assertion the HAVING clause:
SELECT TOP 10 e.empid
FROM employee e
INNER JOIN department d
ON d.depId = e.depid
INNER JOIN employeeProject p
ON p.empid = e.empid
WHERE
p.projectID IN (3,4)
GROUP BY
e.empid
HAVING
MIN(p.projectID) <> MAX(p.projectID);
If the minimum and maximum projectID are not equal for a given employee, after restricting to only projects 3 and 4, then it implies that this employee meets the criteria.
Why not just use select distinct?
select distinct top 10 empid
from employee e inner join
employeeProject p
on p.empid = e.empid
where p.projectID in (3, 4)
order by row_number() over (partition by e.depId order by e.empid);
Note that the department table is not needed.
Alternatively,
select top (10) e.*
from employee e
where exists (select 1
from employeeprojects ep
where p.emdid = e.empid and
p.projectid in (3, 4)
)
order by row_number() over (partition by e.depid order by newid());
I am having trouble writing a query that will select all Skills, joining the Employee and Competency records, but only return one skill per employee, their newest Skill. Using this sample dataset
Skills
======
id employee_id competency_id created
1 1 1 Jan 1
2 2 2 Jan 1
3 1 2 Jan 3
Employees
===========
id first_name last_name
1 Mike Jones
2 Steve Smith
Competencies
============
id title
1 Problem Solving
2 Compassion
I would like to retrieve the following data
Skill.id Skill.employee_id Skill.competency_id Skill.created Employee.id Employee.first_name Employee.last_name Competency.id Competency.title
2 2 2 Jan 1 2 Steve Smith 2 Compassion
3 1 2 Jan 3 1 Mike Jones 2 Compassion
I was able to select the employee_id and max created using
SELECT MAX(created) as created, employee_id FROM skills GROUP BY employee_id
But when I start to add more fields in the select statement or add in a join I get the 'Column 'xyz' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.' error.
Any help is appreciated and I don't have to use GROUP BY, it's just what I'm familiar with.
The error that you were getting is because SQL Server requires any item in the SELECT list to be included in the GROUP BY if there is an aggregate function being used.
The problem with that is you might have unique values in some columns which can throw off the result. So you will want to rewrite the query to use one of the following:
You can use a subquery to get this result. This gets the max(created) in a subquery and then you use that result to get the correct employee record:
select s.id SkillId,
s.employee_id,
s.competency_id,
s.created,
e.id employee,
e.first_name,
e.last_name,
c.id competency,
c.title
from Employees e
left join Skills s
on e.id = s.employee_id
inner join
(
SELECT MAX(created) as created, employee_id
FROM skills
GROUP BY employee_id
) s1
on s.employee_id = s1.employee_id
and s.created = s1.created
left join Competencies c
on s.competency_id = c.id
See SQL Fiddle with Demo
Or another way to do this is to use row_number():
select *
from
(
select s.id SkillId,
s.employee_id,
s.competency_id,
s.created,
e.id employee,
e.first_name,
e.last_name,
c.id competency,
c.title,
row_number() over(partition by s.employee_id
order by s.created desc) rn
from Employees e
left join Skills s
on e.id = s.employee_id
left join Competencies c
on s.competency_id = c.id
) src
where rn = 1
See SQL Fiddle with Demo
For every non-aggregated column you add to your SELECT statement you need to update your GROUP BY to include it.
This article may help you understand why.
;WITH
MAX_SKILL_created AS
(
SELECT
MAX(skills.created) as created,
skills.employee_id
FROM
skills
GROUP BY
skills.employee_id
),
MAX_SKILL_id AS
(
SELECT
MAX(skills.id) as id,
skills.employee_id
FROM
skills
INNER JOIN MAX_SKILL_created
ON MAX_SKILL_created.employee_id = skills.employee_id
AND MAX_SKILL_created.created = skills.created
GROUP BY
skills.employee_id
)
SELECT
* -- type all your columns here
FROM
employees
INNER JOIN MAX_SKILL_id
ON MAX_SKILL_id.employee_id = employees.employee_id
INNER JOIN skills
ON skills.id = MAX_SKILL_id.id
INNER JOIN competencies
ON competencies.id = skills.competency_id
If you are using SQL Server than you can use OUTER APPLY
SELECT *
FROM employees E
OUTER APPLY (
SELECT TOP 1 *
FROM skills
WHERE employee_id = E.id
ORDER BY created DESC
) S
INNER JOIN competencies C
ON C.id = S.competency_id