Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have to write a query where an employee can take advance twice in a year. Now I have to show all the employees who has taken advance one time or two times in fiscal year(July to June).

I have tables like this
Empoyee(EmpId,Name)
Advance(advanceId,AdvanceDate,empId).

Now I have to show columns in form of following output.

empId, Name, 1st advance date, 2nd advance date.

How can I do that ?

SQL
SELECT e.EmployeeId, e.FullName,ap.RequestedOn from Employee e join AdvancePayment ap on e.EmployeeId = ap.EmployeeId
WHERE ap.EmployeeID IN 
(SELECT EmployeeId FROM AdvancePayment 
GROUP BY EmployeeID HAVING COUNT (year(dateadd(M,-6,RequestedOn))) > 1)

This gives me records of all the employees who has taken advance twice in a fiscal year. But i am unable to get that how can I get records with following output
empId, Name, 1st advance date, 2nd advance date.
Posted
Updated 18-Jan-16 1:31am
v4
Comments
OriginalGriff 18-Jan-16 6:55am    
What have you tried?
Where are you stuck?
touseef4pk 18-Jan-16 6:57am    
SELECT e.EmployeeId, e.FullName,ap.RequestedOn from Employee e join AdvancePayment ap on e.EmployeeId = ap.EmployeeId
WHERE ap.EmployeeID IN
(SELECT EmployeeId FROM AdvancePayment
GROUP BY EmployeeID HAVING COUNT (year(dateadd(M,-6,RequestedOn))) > 1)

This gives me records of all the employees who has taken advance twice in a fiscal year. But i am unable to get that how can I get records with following output
empId, Name, 1st advance date, 2nd advance date.

1 solution

If I get what you are asking, here is a possible way of doing it.
SQL
with AdvanceCnt as (
	select 
		EmployeeId,
		RequestedOn,
		row_number over(partition by EmployeeId order by RequestedOn) rowid
	from AdvancePayment
	where RequestedOn
	-- do RequestedOn date filtering here
)
select
	e.EmployeeId empId,
	e.FullName Name,
	ac1.RequestedOn [1st advance date],
	ac2.RequestedOn [2nd advance date]
from Employee e
inner join
	(select * from AdvanceCnt where rowid = 1) ac1
	on e.EmployeeId = ac1.EmployeeId
left join
	(select * from AdvanceCnt where rowid = 2) ac2
	on e.EmployeeId = ac2.EmployeeId
;

The query has not been tested, but hopefully it will help you out.
 
Share this answer
 
v3
Comments
_Asif_ 18-Jan-16 7:57am    
Tested version of your solution.

DECLARE @EMP TABLE
(
EmpId int,
Name varchar(10)
)
;

DECLARE @Advance TABLE
(
advanceId int,
empId int,
AdvanceDate DateTime
)
;

INSERT INTO @EMP (EmpId, Name)
SELECT 1, 'A'
UNION ALL
SELECT 2, 'B'
UNION ALL
SELECT 3, 'C'
;

INSERT INTO @Advance (advanceId, empId, AdvanceDate)
SELECT 1, 1, '01-Jan-2015'
UNION ALL
SELECT 1, 1, '01-Jan-2016'
UNION ALL
SELECT 3, 1, '01-Feb-2016'
UNION ALL
SELECT 4, 2, '01-Jan-2016'
;

with AdvanceCnt as (
select
EmpId,
AdvanceDate,
ROW_NUMBER() over(partition by EmpId order by AdvanceDate) as rowid
from @Advance
WHERE AdvanceDate between '01-Jun-2015' AND '31-Jul-2016'
)
SELECT E.EMPID,
E.NAME, AC1.AdvanceDate as AdvanceDate1, AC2.AdvanceDate as AdvanceDate2
from @EMP E
LEFT OUTER JOIN AdvanceCnt AC1 on E.EMPID = AC1.EMPID AND AC1.ROWID = 1
LEFT OUTER JOIN AdvanceCnt AC2 on E.EMPID = AC2.EMPID AND AC2.ROWID = 2
_Asif_ 18-Jan-16 7:57am    
+5
jaket-cp 18-Jan-16 8:00am    
thanks, and also thanks for testing :)
touseef4pk 18-Jan-16 8:21am    
Thanks. This is exactly what I require.
jaket-cp 18-Jan-16 8:25am    
you are welcome, glad to be of help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900