So basically I have a students table and a discipline table. Each row of the discipline table is a student id, date and columns we call “jugs” and “demerits”. I’m trying to build a view that would allow a user to search by date any students that have a matching record in the discipline table for that date and give me the total demerits and jugs for ALL the dates for that student (GROUP BY student id). I cannot however do this because adding the date to the select statement breaks the group by. Is there a way to add the date search into my asp.net maker app by declaring variables? Something like this.
DECLARE @StartDate DATE
DECLARE @EndDate DATE
SELECT
s.ID AS StudentID,
s.FIRST_NAME,
s.LAST_NAME,
SUM(d.Demerits) AS TotalDemerits,
SUM(d.Jugs) AS TotalJugs
FROM
[dbo].[viewStudents] s
JOIN
(
SELECT *
FROM [dbo].[Discipline]
WHERE givenDate >= @StartDate
AND givenDate <= @EndDate
) d ON s.ID = d.Student
GROUP BY
s.ID, s.FIRST_NAME, s.LAST_NAME
ORDER BY
s.LAST_NAME, s.FIRST_NAME;