Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have gridview with name and number of days in month as column.I want add a extra column which will show number of present days of every employee in a month


like

Name 1/4/14 2/4/14 3/4/14 Tot
Sandip P P A 2
Sumit A A P 1
Posted
Comments
Nelek 10-Apr-14 7:11am    
What have you tried?[^] Where are you getting problems?
Sandip Paul 491984 10-Apr-14 7:14am    
i did the first part by pivot.but i am not able to do second part i,e i need to show the count the present days.
Ajith K Gatty 10-Apr-14 7:17am    
Hi,
from where you are binding data to grid?
Sandip Paul 491984 10-Apr-14 7:19am    
from pivot.Here is my stored procedure

ALTER procedure [dbo].[EmpAttandance] @iMon int,@iyear int
as
begin
-- Getting all distinct dates into a temporary table #Dates
SELECT DISTINCT ADate INTO #Dates
FROM AttandanceTran where DATEPART(MM,ADate) =@iMon and DATEPART(YYYY,ADate)=@iyear
ORDER BY ADate


-- The number of days will be dynamic. So building
-- a comma seperated value string from the dates in #Dates
DECLARE @cols NVARCHAR(4000)
SELECT @cols = COALESCE(@cols + ',[' + CONVERT(varchar, Adate, 106)
+ ']','[' + CONVERT(varchar, Adate, 106) + ']')
FROM #Dates
ORDER BY Adate

-- Building the query with dynamic dates
DECLARE @qry NVARCHAR(4000)
SET @qry =
'SELECT * FROM
(SELECT Empcode,Name,Type,Adate,count(type) over(PARTITION BY empcode) AS Tot
FROM AttandanceTran INNER JOIN Empmast ON AttandanceTran.ECode=Empmast.ECode
where DATEPART(MM,ADate)=' + CAST(@imon as varchar(10)) + '
and DATEPART(YYYY,ADate)=' + CAST(@iyear as varchar(10)) + ' )emp
PIVOT (Max(Type) FOR ADate IN (' + @cols + ')) AS Type'


-- Executing the query
EXEC(@qry)


-- Dropping temporary tables
DROP TABLE #Dates

end

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