Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

I want employee attendence reports details monthwise.
This is the data format.

E.No    Name       Status       Date
----    -------    -------   ----------
225     Arun          A      08/25/2011
226   Jegankumar      P      08/25/2011
225     Arun          P      08/24/2011
226   Jegankumar      P      08/24/2011
225     Arun          A      08/23/2011
226   Jegankumar      P      08/23/2011



how can i get the below format.

E.No    Name          23-O4-13     24-04-13     25-04-13
----    -----         --------     --------     --------
225     Arun           A              P             A
226    Jegankumar      P              P             P


please help me any one.
Posted
Updated 5-Jun-13 1:38am
v2
Comments
CHill60 5-Jun-13 6:35am    
Use the Improve question link to show the code that you are using to extract from the table so far

 
Share this answer
 
Try this:
SQL
DECLARE @cols NVARCHAR(200)
DECLARE @dt NVARCHAR(2000)
DECLARE @pt NVARCHAR(MAX)


SET @cols = STUFF((SELECT DISTINCT '],[' + CONVERT(NVARCHAR(10),[Date],120)
					FROM [Table1] 
					ORDER BY '],[' + CONVERT(NVARCHAR(10),[Date],120)
			FOR XML PATH('')),1,2,'') + ']'

SET @dt = 'SELECT * FROM Table1'

SET @pt = 'SELECT [E.No], [Name], ' + @cols + ' ' +
        'FROM (' + @dt + ') AS DT ' +
        'PIVOT(MAX([Status]) FOR [Date] IN (' + @cols + ')) AS PT'

EXEC(@pt)
 
Share this answer
 
you can use pivot syntax in MS sql
 
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