student_name id attendance dates
--------------------------------------------------------------------
abc 1 p 2015-12-29 11:58:29.143
def 2 a 2015-12-28 11:58:29.143
ghk 3 p 2015-12-27 11:58:29.143
---------------------------------------------------------------------
这里我需要打印如下
student_name id month 1 2 3 4 5 6 7 8 9 10....31
------------------------------------------------------------
abc 1 jan p f p p p p p p...............
pqr 2 feb p p p p p p p p ...........
请帮我写这样的剧本。 我必须每月打印每个学生每月的出勤率
答案 0 :(得分:1)
您可以使用以下sql查询以您询问的格式获取输出:
SELECT * FROM(
SELECT [Name]
,[Id]
,[attendance]
,DATENAME(M, [date])as [MonthValue]
,DAY([date]) as [DayValue]
FROM [students]) as stud
PIVOT
(
MAX([attendance])
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 PivotTable;