我有一个表(ClassEnrolments),其中包含学生数据:
StudentId Student Module Status EndDate Credits
12345678 J Bloggs Introduction Pass 2014/09/01 10
12345678 J Bloggs Advanced Pass 2014/06/01 15
23456789 T Guy Introduction Pass 2013/05/25 10
23456789 T Guy Advanced Pass 2014/03/21 15
我想要做的是返回学生在一段时间内总共采取了多少模块的信息,例如,如果我们采用上述数据并查看01/01/2014至24期间/ 11/2014它将返回以下内容(基于EndDate)
StudentId Student Modules Credits AnnivDate
12345678 J Bloggs 2 25 2015/06/01
23456789 T Guy 1 15 2014/05/25
这是使用SQL Server 2008 - 非常感谢任何帮助。
我现在也想知道是否使用相同的场景 - 是否可以计算从这些模块中获得的学分数?
另一个想法!根据学生最重要的模块,看一些周年纪念日会很有用。这将有助于确定学生是否在周年日之前获得了正确的学分。我已经玩弄了DATEADD功能的想法,但这似乎又重复了学生的信息。
DATEADD(MONTH, DATEDIFF(MONTH, 0, EndDate()) +12,0) AS AnnivDate
我理解为什么,因为会有多个实例,但有没有办法查看EndDate的第一个实例?
答案 0 :(得分:1)
试试这个:
Set dateformat DMY;
Select StudentId, Student, Count(Modules) as ModuleCount
from ClassEnrolments
Where EndDate >= convert(Datetime, '01-01-2014', 105) and
EndDate <= convert(Datetime, '24-11-2014', 105)
Group By StudentId, Student
答案 1 :(得分:0)
试试这个:
SELECT
StudentId,
Student,
COUNT(*) Modules
FROM ClassEnrolments
WHERE
EndDate BETWEEN '2014-01-01' AND '2014-11-24'
GROUP BY
StudentId,
Student
答案 2 :(得分:0)
SELECT StudentID, Student, Count(module) AS Modules
FROM ClassEnrolments
GROUP BY StudentID, Student
日期
SELECT StudentID, Student, Count(module) AS Modules
FROM ClassEnrolments
WHERE EndDate BETWEEN '2014-01-01' AND '2014-11-24'
GROUP BY StudentID, Student
答案 3 :(得分:0)
使用Group By
和汇总功能count
计算Module
并使用Between operator
过滤特定日期范围之间的数据来过滤数据
SELECT StudentId,
Student,
Count(Module) Module,
Sum(Credits) Credits
FROM tablename
WHERE EndDate BETWEEN '2014-01-01' AND '2014-11-24'
GROUP BY StudentId,
Student
答案 4 :(得分:0)
SELECT
StudentId,Student,COUNT(*)
FROM TableName
WHERE EndDate BETWEEN '2014-01-01' AND '2014-11-24'
AND Status = 'Pass'
GROUP BY StudentId,Student
答案 5 :(得分:0)
实际上并不是你要求的,但可能是另一种选择,具体取决于你想要的东西。
DECLARE @ClassEnrollments
TABLE (
StudentId INT
,Student VARCHAR(50)
,Module VARCHAR(50)
,Status VARCHAR(50)
,EndDate DATE
)
INSERT
INTO @ClassEnrollments
VALUES (12345678,'J Bloggs','Introduction','Pass','2014/09/01')
,(12345678,'J Bloggs','Advanced','Pass','2014/06/01')
,(23456789,'T Guy','Introduction','Pass','2013/05/25') -- Excluded
,(23456789,'T Guy','Advanced','Pass','2014/03/21')
SELECT StudentId
,Student
,Module
,Status
,EndDate
,COUNT(Module) OVER (PARTITION BY studentId) as #Modules
FROM @ClassEnrollments
WHERE EndDate
BETWEEN '2014-01-01'
AND '2014-11-24'
ORDER BY
StudentId
,Module