0

I m using SQL 'Pivot' to show Student Attendance on monthly basis but facing Error Invalid column name 'DayValue'. Invalid column name 'Stu_Status'.

M using following SQL Query

Select 
       StudentID, Stu_Status, DAY([AttandanceDate]) as [DayValue] 
  From tbl_Students_Attandance as studAttd
 PIVOT (Max([Stu_Status])
   FOR [DayValue] IN ([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])
       ) AS Piviottable;

using below table Design

SQL Table Design Image

To achieve below result Required result Image

Prabhat Sinha
  • 1,500
  • 20
  • 32
Zain Ali
  • 81
  • 9

2 Answers2

0

You can use conditional aggregation instead :

select StudentID, max(case when day(AttandanceDate) = 1 then Stu_Status end) as [1],
       . . . 
from  tbl_Students_Attandance as studAttd
group by StudentID;

However, i suspect this will also require to mention year() to match current year & month attendance.

Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
0

TRY THIS: There are few issues in your query so if it works then compare both queries and try to understand what you just missed

SELECT StudentID, [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]
FROM(
    Select 
       StudentID, Stu_Status, DAY([AttandanceDate]) as [DayValue] 
  From tbl_Students_Attandance
) AS studAttd
PIVOT
(
    MAX(Stu_Status)
    FOR [DayValue] IN ([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])
) AS pvt
Shushil Bohara
  • 5,556
  • 2
  • 15
  • 32