我正在使用SQL'Pivot'来显示每月的出勤率,但要面对Error Invalid column name 'DayValue'. Invalid column name 'Stu_Status'
。
M使用以下SQL查询
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;
使用下表设计
要达到以下效果 Required result Image
答案 0 :(得分:0)
您可以改用有条件的聚合:
select StudentID, max(case when day(AttandanceDate) = 1 then Stu_Status end) as [1],
. . .
from tbl_Students_Attandance as studAttd
group by StudentID;
但是,我怀疑这还需要提及year()
以匹配当前year
和month
的出勤率。
答案 1 :(得分:0)
请尝试:查询中的问题很少,因此如果可以,请比较两个查询并尝试了解您刚刚错过的内容
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