Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to fetch the number of days a particular employee is present from a table. Since i should also take the current date and find the starting day of the month and for that days it should display the present count.
Posted
Comments
CPallini 14-Sep-15 4:07am    
So? What is your doubt?

1 solution

Hello ,
here is a sample .
--First create one temporary table .
CREATE TABLE #TMP  
(  
   NAME VARCHAR(50),  
   DATE DATETIME ,  
   PRESENT_STATUS VARCHAR(10)  
)  

--insert data 
INSERT INTO #TMP VALUES ('A','2016-07-01','PRESENT')  
INSERT INTO #TMP VALUES ('M','2016-07-01','PRESENT')  
INSERT INTO #TMP VALUES ('A','2016-07-02','ABSENT')  
INSERT INTO #TMP VALUES ('M','2016-07-02','ABSENT')  
INSERT INTO #TMP VALUES ('A','2016-07-03','PRESENT')  
INSERT INTO #TMP VALUES ('M','2016-07-03','PRESENT')  
INSERT INTO #TMP VALUES ('A','2016-07-04','ABSENT')  
INSERT INTO #TMP VALUES ('M','2016-07-04','PRESENT')  


--NOW DECLARE ONE DATETIME FOR START DATE OF THE MONTH
DECLARE @STARTDATE DATETIME
SET @STARTDATE=(SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))



--NOW SIMPLE SELECT THE COUNT OF PRESENT STATUS
SELECT COUNT(1) AS PRESENT_COUNT , NAME FROM #TMP WHERE PRESENT_STATUS='PRESENT' 
AND DATE BETWEEN @STARTDATE AND @ENDATE --HERE @ENDATE WILL BE THE LAST DATE
GROUP BY NAME


Thanks
 
Share this answer
 

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