Skip to content
Advertisement

SQL Server query to show summary list for particular months or between dates

I have an attendance table which contains student’s attendance date and attendance status.

Is there any way to display a summary something like this:

Student 20-09-2012 21-09-2012 22-09-2012 23-09-2012
  xyz       P          A          P           P
  abc       P          P          P           P

Through a SQL Server query?

I have tried with the PIVOT but it is not working because of aggregate function.

Advertisement

Answer

I have tried with PIVOT but it not working because of aggregate function.

You can use MAX as an aggregate function like so:

SELECT t.StudentName,
  MAX(CASE WHEN t.Date = '20120920' THEN t.Status END) AS '20-09-2012',
  MAX(CASE WHEN t.Date = '20120921' THEN t.Status END) AS '21-09-2012',
  MAX(CASE WHEN t.Date = '20120922' THEN t.Status END) AS '22-09-2012',
  MAX(CASE WHEN t.Date = '20120923' THEN t.Status END) AS '23-09-2012'
FROM Attendence t
GROUP BY t.StudentName
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement