2

I have a table Employees, which has Fields as below:

Employee_name,Employee_id,Employee_status,Employee_loc,last_update_time.

This table does not have any constraint. I have tried the below query.

select Employee_name, count(1) 
from Employees 
where Employee_status = 'ACTIVE' 
Group by Employee_name,Employee_loc 
having count(Employee_name) > 1 
order by count(Employee_name)  desc

In the select, I need to get Employee_id too.. Can any one help on how to get that?

didierc
  • 14,572
  • 3
  • 32
  • 52
Muthukumar
  • 93
  • 2
  • 3
  • 10
  • 6
    If there are ten employees with the same name and location, which `employee_id` do you want? Do you want all 10? Just the min (or the max)? If you want all 10, do you want 10 rows to be returned? – Justin Cave Apr 03 '13 at 10:24
  • 1
    Can you provide sample data & required output? – Zo Has Apr 03 '13 at 10:27
  • I need all the 10 employee_ids. ( Ideally it will get the employee list having same location and name with Active status.) – Muthukumar Apr 03 '13 at 10:56
  • Thanks Justin, made something click for me. – Shawn Jan 10 '14 at 19:04

3 Answers3

4

You can just add Employee_id to the query, and also add it to the group by clause. (Adding it to the grouping won't make any difference in the query results, assuming each employee name each employee id is unique).

If the grouping does make a difference, that implies that some combinations of employee name and location have more than one ID associated with them. Your query would therefore need to decide which ID to return, possibly by using an aggregate function.

  • I cannot,, If i add employee_id also in group by, then it is trying to find the unique combination of all the three fields( Employeed_name,Employee_loc, Employee_id).. I need the unique combination for Employee_name and Employee_loc.. – Muthukumar Apr 03 '13 at 10:57
  • @Muthukumar, if it makes a difference this means that some combinations of `employee_name, empolyee_loc` have more than one employee_id. Then how do you know which one you want? –  Apr 03 '13 at 11:00
  • I need all those employee_ids.. Ideally it will get the employee list having same location and name with Active status – Muthukumar Apr 03 '13 at 11:07
1
SELECT EMPLOYEE_NAME, EMPLOYEE_ID, COUNT(1)
FROM
EMPLOYEES
WHERE
EMPLOYEE_NAME IN
(
SELECT EMPLOYEE_NAME
FROM EMPLOYEES
WHERE Employee_status = 'ACTIVE'
GROUP BY Employee_name,Employee_loc
HAVING COUNT(*) > 1
)
GROUP BY EMPLOYEE_NAME, EMPLOYEE_ID
Santhosh
  • 1,771
  • 1
  • 15
  • 25
  • Great.. thats cool. now for each employee_name , I have multiple rows. I just need to get the latest one by updated time? it is possible to get that? – Muthukumar Apr 03 '13 at 11:06
  • We need to Employee_status in the outer query, as it is again pulling employee with all status.. – Muthukumar Apr 03 '13 at 12:20
0

You can also use partition by clause and select whichever columns you want to see irrespective of the columns you are using for aggregation.

A very short and simple explanation here - Oracle "Partition By" Keyword

Community
  • 1
  • 1
KLeonine
  • 167
  • 2
  • 10