| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | SufarlinStarting Member
 
 
                                        2 Posts | 
                                            
                                            |  Posted - 2014-03-04 : 04:22:56 
 |  
                                            | Need help...javascript:insertsmilie('  ')javascript:insertsmilie('  ')I have a "checkInout" table with a structure like the following, I took the  "checkinout" table from the database attendance machine.UserID (Int)CheckTime (datetime)CheckType (nvarchar(1))UserID         -;- CheckTime  -;-       CheckType349	-;- 2014-02-25 08:00:28.000	 -;- I349	-;- 2014-02-25 17:10:13.000	-;- O912	-;-  2014-02-25 07:49:44.000	-;- I912	-;- 2014-02-25 17:50:47.000	-;- O919	-;- 2014-02-25 08:00:49.000	-;- I919	-;- 2014-02-25 17:07:21.000	-;- O919	-;- 2014-02-26 07:50:01.000	-;- I919	-;- 2014-02-26 07:59:16.000	-;- I919	-;- 2014-02-27 08:01:46.000	-;- I919	-;- 2014-02-27 17:30:58.000	-;- O919	-;- 2014-02-27 17:29:54.000	-;- I919	-;- 2014-02-27 17:35:15.000	-;- OI want to get a result like thisUserId -;- 2014-02-25 -;- 2014-02-26 -;- 2014-02-27349     -;-    P -;- A -;- A912     -;-    P -;- A -;- A919     -;-    P -;- A -;- A919     -;-    A -;- NCO -;- A919     -;-    A -;- A  -;- Pwith this condition1. if (Min (CHECKTIME) where CHECKTYPE = I) - (Max (CHECKTIME) where CHECKTYPE = O)> 8:00:00 (total hours) then "P"2. if (Min (CHECKTIME) where CHECKTYPE = I) - (Max (CHECKTIME) where CHECKTYPE = O) <08:00:00 (total hours) then "A"P = PresentA = Not PresentNCO = No Check Out 08:00:00 is the number of hours workedI've tried various ways, but did not get satisfactory resultscan anyone willing to help me? I'm building HR management and I stopped in this issueI am waiting for your reply, you can reply via this forum or directly to my email sufarlin@yahoo.comThank you for your attention,Sufarlin |  |  
                                    | khtanIn (Som, Ni, Yak)
 
 
                                    17689 Posts | 
                                        
                                          |  Posted - 2014-03-04 : 04:58:47 
 |  
                                          | [code];withcte as(	select	UserID, 		CheckDate 	= dateadd(day, datediff(day, 0, CheckTime), 0), 		CheckDuration	= datediff(minute, 							min(case when CheckType = 'I' then CheckTime end), 							max(case when CheckType = 'O' then CheckTime end))					from	checkInout	group by UserID, dateadd(day, datediff(day, 0, CheckTime), 0)),cte2 as(	select	UserID,		CheckDate,		Attendance 	= case 	when CheckDuration is null then 'NCO'					when CheckDuration >= 8 * 60 then 'P'					when CheckDuration <  8 * 60 then 'A'					end					from	cte)select	UserID, 	[2014-02-25]	= isnull([2014-02-25], 'A'), 	[2014-02-26]	= isnull([2014-02-26], 'A'), 	[2014-02-27]	= isnull([2014-02-27], 'A')from	cte2 c	pivot	(		max(Attendance)		for CheckDate in ([2014-02-25], [2014-02-26], [2014-02-27])	) p[/code] KH[spoiler]Time is always against us[/spoiler]
 |  
                                          |  |  |  
                                    | SufarlinStarting Member
 
 
                                    2 Posts | 
                                        
                                          |  Posted - 2014-03-04 : 05:24:47 
 |  
                                          | quote:thank you for your answer, your answer is very precious to me. once again thank you. but there is one more question that makes me confused. how to display it in crystal report with checkdate conditions for one month while the number of days in a month sometimes there are 31 days, 28 days, 30 days?SufarlinOriginally posted by khtan
 
 ;withcte as(	select	UserID, 		CheckDate 	= dateadd(day, datediff(day, 0, CheckTime), 0), 		CheckDuration	= datediff(minute, 							min(case when CheckType = 'I' then CheckTime end), 							max(case when CheckType = 'O' then CheckTime end))					from	checkInout	group by UserID, dateadd(day, datediff(day, 0, CheckTime), 0)),cte2 as(	select	UserID,		CheckDate,		Attendance 	= case 	when CheckDuration is null then 'NCO'					when CheckDuration >= 8 * 60 then 'P'					when CheckDuration <  8 * 60 then 'A'					end					from	cte)select	UserID, 	[2014-02-25]	= isnull([2014-02-25], 'A'), 	[2014-02-26]	= isnull([2014-02-26], 'A'), 	[2014-02-27]	= isnull([2014-02-27], 'A')from	cte2 c	pivot	(		max(Attendance)		for CheckDate in ([2014-02-25], [2014-02-26], [2014-02-27])	) p KH[spoiler]Time is always against us[/spoiler]
 
 |  
                                          |  |  |  
                                |  |  |  |  |  |