I have a table with following columns
Emp_id
Work_date
element
I want a query (modified version of below query) which will return a single row if there are more than 1 row for given work_date and employee id (GROUP BY EMP_ID and WORK_DATE) in above table. So I have written query like:
SELECT EMP_ID, WORK_DATE FROM myTable
where WORK_DATE = :p_WorkDate
GROUP BY EMP_ID, WORK_DATE
HAVING COUNT (1) > 1
For example:
EMP_ID WORK_DATE
1 1/1/2013
1 1/1/2013
2 1/1/2013
Query should return as follows if I pass 1/1/2013 for :p_WorkDate:
1 1/1/2013
Basically I am trying to find if there are more than 1 row by EMP_ID and WORK_DATE but there is additional requirement what ELEMENT column contains - if it contains values from a set (element1 + element2) or (element3 + element4).
The additional requirements (following) depend upon what element column contains (if the rows have values from set).
1) There are 2 rows with same emp_id and work_date and one of the element column is element1 and other element column is element2 (a set of element1 and element 2)
For example:
EMP_ID WORK_DATE element
1 1/1/2013 element1
1 1/1/2013 element2
the query should not return any row because even if there are two rows, it is a set (element1 and elelement2)
2) There are 2 rows with same emp_id and work_date and one of the element column is element3 and other element column is element4 (a set of element3 and element 4)
For example:
EMP_ID WORK_DATE element
1 1/1/2013 element3
1 1/1/2013 element4
The query should not return any row because even if there are two rows, it is a set (element3 and element 4)
3) If there are 2 rows with same emp_id and work_date and which is not a set as above should return a row
For example:
EMP_ID WORK_DATE element
1 1/1/2013 element1
1 1/1/2013 xxx
(same result if element column had element2 instead of element1)
Query should return as follows if I pass 1/1/2013 for :p_WorkDate:
1 1/1/2013
4) If there are more than 2 rows with same emp_id and work_date, no matter what element column contains, it should return a row.
For example:
EMP_ID WORK_DATE element
1 1/1/2013 element1
1 1/1/2013 element2
1 1/1/2013 xxx
Query should return as follows if I pass 1/1/2013 for :p_WorkDate:
1 1/1/2013
Thank you