Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.60/5 (3 votes)
Task is to generate a Monthly Report
Table: tblAttendance
Code Name AttendanceDate Status
---- ---- -------------- ------
A001 Abc Jan-01-2013 Present
B001 Xyz Jan-01-2013 Present
A001 Abc Jan-02-2013 Absent
B001 Xyz Jan-02-2013 Late
A001 Abc Jan-03-2013 Late
.
.


Now I have to create Monthly Report in Format given below
Jan, 2013
Code Name 1 2 3 .. 29 30 31
---- ---- - - - .. -  -  -
A001 Abc  P A L .. L  P  A 
B001 Xyz  P L L .. L  P  A 
.
.


Problem: Not getting any ideas to design the above report
Posted
Updated 18-Apr-17 3:22am
v4
Comments
Devang Vaja 5-Mar-13 7:49am    
You can do it with html reports easily
tbl=new htmltable()
tr=new htmltableRow()
tc=new htmltablecell()
tc.bgcolor="lightgreen"
tc.innertext=ds.tables(0).rows(i)("Att")
tr.cells.add(tc)
tbl.rows.add(tr)

and also main thing is You can customize each cell in loop
But you have to develop logic for that as per your requirement
i have made this report customize as if employee is in half leave i have shown his cell half green,sundays as red...
You can work it out..
SHUBH CODING:)
pryashrma 5-Mar-13 7:54am    
i don't want to use html reports... but your idea of customizing cell is great! Thanks
Devang Vaja 5-Mar-13 7:57am    
Your welcome..:)

1 solution

for this I recommend you to use the CROSSTAB query. In SQL PIVOT table.
http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx[^]

http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx[^]

try your self. If you have any question feel free to ask.


-------------------------

Problem with your PIVOT aggregation. you can use something line MAX([Status])
SQL
SELECT [EmpCode], [ProjectName],[1] , [2],[3],[4],[5]
FROM 
(SELECT Day([AttendanceDate]) as d1, [EmpCode],[ProjectName],[AttendanceDate] , [Status]
FROM [tebs].[dbo].[View_Attendance]) p
PIVOT(
MAX([Status])
FOR d1 IN (  [1] , [2],[3],[4],[5]) ) AS pvt
ORDER BY 
pvt.[EmpCode],  pvt.[ProjectName];
 
Share this answer
 
v2
Comments
pryashrma 5-Mar-13 23:59pm    
Thanks for the suggestion, I tried but sql express is throwing following error:Incorrect syntax near the keyword 'FOR'.

I have no idea for resolving the error

Query:

SELECT [EmpCode], [ProjectName],[1] , [2],[3],[4],[5]
FROM
(SELECT Day([AttendanceDate]) as d1, [EmpCode],[ProjectName],[AttendanceDate] , [Status]
FROM [tebs].[dbo].[View_Attendance]) p
PIVOT(
[Status]
FOR d1 IN ( [1] , [2],[3],[4],[5]) ) AS pvt
ORDER BY
pvt.[EmpCode], pvt.[ProjectName];

Could u plz help!
Tharaka MTR 6-Mar-13 2:52am    
What is your SQL express version? 2005? 2008? etc..
Tharaka MTR 6-Mar-13 3:16am    
I have updated my answer by modifying your query. please check.
pryashrma 6-Mar-13 5:35am    
Thanks for replying!

Problem not yet resolved, now the query is generating n rows for n attendance dates for each employee... Any suggestions...
EmpCode ProjectName 1 2 3 4 5
------- ----------- - - - - -
A100 p1 NULL NULL P NULL NULL
A100 p1 NULL NULL NULL P NULL
A100 p1 NULL NULL NULL NULL P
A100 p1 NULL NULL NULL NULL NULL
A101 p3 NULL NULL P NULL NULL
A101 p3 NULL NULL NULL P NULL
A101 p3 NULL NULL NULL NULL L
A102 p3 NULL NULL P NULL NULL
A102 p3 NULL NULL NULL P NULL
A102 p3 NULL NULL NULL NULL A
.
.

and I more request.... could u plz explain why ' max(status) ' was used????
Member 13427569 4-Oct-17 3:23am    
In your code remove [AttendanceDate] near select statement-->(SELECT Day([AttendanceDate]) as d1, [EmpCode],[ProjectName] , [Status]
FROM [tebs].[dbo].[View_Attendance]) it has to be like above

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