In a SQL Server 2012 database, I am supposed to count the number of times each 'canned' message is used for elementary students in the last school year and the current school year.
Right now I have the following T-SQL that kind of works:
USE TEST
SELECT
GS.Comments, COUNT(*) AS [Counts]
FROM
dbo.Enrol Enrol
JOIN
dbo.Student Student ON Student.StudentID = Enrol.StudentID
JOIN
dbo.GS GS ON GS.StudentID = Student.Studentid
AND (GS.Comments IS NOT NULL)
AND (GS.Comments <> '')
WHERE
Enrol.grade IN ('KG', '01', '02', '03', '04', '05', '06')
AND Enrol.endYear BETWEEN 2016 AND 2017
GROUP BY
GS.Comments
ORDER BY
Counts DESC, GS.Comments ASC
The problem is the GS.Comments
column is defined as varchar(1200)
. There can be one message in the column and/or there can be lots of messages in this column. Each message ends with a period and there is a space between each message.
An example of multiple messages in the one GS.Comments
column would look like the following:
The student is trying hard and needs to make their time more efficiently. This student is good at math. This student turns in their assignments on time. This student seems to enjoy school.
An example of when one messages is in the one GS.Comments
column would look like the following:
This student seems to enjoy school.
Thus would show me the T-SQL logic that I can use when the GS.Comments
column contains multiple messages and/or just one message so that I can count the number of times each unique message has been used?