Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends

I have Attendance table in that I have Columns: EmpCode,EmpName,1,2,3.......upt0 31(days).I am dumping attendance from excel sheet.

Ex:
EmpCode EmpName 1 2 3
1013 Ch.Krishna Mohan A P P

Now what I am looking for is

loop columns from 1 to 31 and find no.of present days and no.of abscent days count.

o/p:
EmpCode EmpName No.ofpresentdays No.ofAbscentDays

1013 Ch.Krishna Mohan 2 1

how to acheive this in sqlserver??

What I have tried:

SELECT 'select '
+ QUOTENAME(c.name)
+ ',count(*) from [dbo].[EmpAttendance] group by ';
+ QUOTENAME(c.name)

FROM sys.columns c
WHERE c.object_id = OBJECT_ID(dbo.EmpAttendance;)

select [1],count(*) from [dbo].[EmpAttendance] where Empcode=1020 group by [1]
Posted
Updated 18-Oct-16 19:25pm

1 solution

Please try this
C#
SELECT
   EmpCode,EmpName,[No.ofpresentdays],[No.ofAbscentDays]
FROM
   dbo.attendance T
   CROSS APPLY (
      SELECT Count(*)
      FROM (VALUES ([1]),	([2]),	([3]),	([4]),	([5]),	([6]),	([7]),	([8]),	([9]),	([10]),	([11]),	([12]),	([13]),	([14]),	([15]),	([16]),	([17]),	([18]),	([19]),	([20]),	([21]),	([22]),	([23]),	([24]),	([25]),	([26]),	([27]),	([28]),	([29]),	([30]),	([31])) C (Val)
      WHERE Val = 'P'
   ) A ([No.ofpresentdays])
    CROSS APPLY (
      SELECT Count(*)
     FROM (VALUES ([1]),	([2]),	([3]),	([4]),	([5]),	([6]),	([7]),	([8]),	([9]),	([10]),	([11]),	([12]),	([13]),	([14]),	([15]),	([16]),	([17]),	([18]),	([19]),	([20]),	([21]),	([22]),	([23]),	([24]),	([25]),	([26]),	([27]),	([28]),	([29]),	([30]),	([31])) C (Val)
      WHERE Val = 'A'
   ) P ([No.ofAbscentDays])
 
Share this answer
 
v3
Comments
prasanna204 19-Oct-16 1:38am    
Thank you so much manu_dhobale
manu_dhobale 19-Oct-16 2:18am    
You're welcome.

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