Employee Table
Id Name
-------------
1 Joy
2 Moni
3 Evan
4 farhad
Absent table
Date(y/m/d) Id
-----------------
2015/1/1 1
2015/1/3 1
2015/1/4 1
2015/1/5 1
2015/1/1 2
2015/1/4 2
2015/1/5 2
2015/1/5 3
I have need data from above two tables as like as bellow
Name Date
Joy 2015/1/5, 2015/1/4, 2015/1/3
Moni 2015/1/5, 2015/1/4
Evan 2015/1/5
Point 1: I will not take date 2015/1/1 because date 2015/1/2 is missing for employee id '1' For Joy in Date Table
Point 2: I will not take date '2015/1/1' because date '2015/1/3' and '2015/1/2' is missing for employee id '2' for moni in date Table
I have tried the problem like this bellow, this worked fine for my problem but its take two much execution time for big data. How can i do that another way so that i will get minimum execution time.
CODE
select a.Id,a.name , [dbo].[hello] ('2015/1/1','2015/1/5',a.Id) From
Employee a
ALTER FUNCTION [dbo].[hello](@start datetime,@End datetime,@Id int)
returns varchar(1111)
AS
begin
declare
@TempDate DateTime,
@CombainedDate varchar(1111)= '',
while(@End>=@start)
begin
select @ TempDate = (select distinct Date from Absent d where Date=@End and EmployeeId=@Id)
if
@ TempDate = @End
begin
set @End = DATEADD(day, -1, @End)
set @ CombainedDate += ',' + cast(@TempDate as varchar(1111))
end
else
begin
break
end
end
return @ CombainedDate
end
2015/1/1 2 2015/1/4 2 2015/1/5 2 2015/1/8 2 2015/1/9 2?