Sql server SQL Server 2012在一个varchar(1200)中拆分多个值

Sql server SQL Server 2012在一个varchar(1200)中拆分多个值,sql-server,tsql,sql-server-2012,Sql Server,Tsql,Sql Server 2012,在SQL Server 2012数据库中,我应该计算上一学年和当前学年每个“罐装”消息用于小学生的次数 现在,我有以下T-SQL这种工作方式: 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

在SQL Server 2012数据库中,我应该计算上一学年和当前学年每个“罐装”消息用于小学生的次数

现在,我有以下T-SQL这种工作方式:

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
当一条消息位于one
GS.Comments
列中时的示例如下所示:

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.
This student seems to enjoy school.

因此,当
GS.Comments
列包含多条消息和/或仅包含一条消息时,我可以使用T-SQL逻辑,以便我可以计算每个唯一消息被使用的次数?

您可以使用以下链接按时段拆分列。然后,在新形成的列上创建一个简单的GROUPBY应该可以让您对其进行计数


您可以使用以下链接按时段拆分列。然后,在新形成的列上创建一个简单的GROUPBY应该可以让您对其进行计数


你能给我一个使用拆分函数的更具体的例子吗?你能给我一个使用拆分函数的更具体的例子吗?
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON

Alter proc [dbo].[StringSplitIntoRows]
(
@tbl varchar(100),---table name as parameter
@col varchar(100)---column name as parameter
)
As
Begin
--creating two temp tables

If OBJECT_ID('tempdb..#test1') is not null drop table #test1
create table #test1(tempcol varchar(200))

--inserting the table(has comma seperated string column) data into temp table 

    Declare @tempresult nvarchar(500)
    set @tempresult = 'insert into #test1(tempcol) select ' + quotename(@col) + ' from ' + quotename(@tbl)
    exec sp_executesql @tempresult

If OBJECT_ID('tempdb..#test2') is not null drop table #test2
            create table #test2(tempcol1 varchar(200))

    Declare @Name varchar(200)
    Declare @real VARCHAR(100) 
    declare split cursor for ---declared a cursor to fetch  row by row data 
            select tempcol from #test1 --temp table which has comma seperated string in column
open split 
        fetch next from split into  @Name

    while (@@FETCH_STATUS=0)
    Begin
            declare @temp int=0
            declare @result nvarchar(MAX)=''
            declare @begin int=0    

        while CHARINDEX(',',@Name,@begin) > 0
        begin
            set @temp=CHARINDEX(',',@Name,@begin)

            set @result=SUBSTRING(@Name,@begin,@temp-@begin)                
            set @begin=@temp+1
            insert into #test2(tempcol1) values(@result)    
        end

        set @real = SUBSTRING(@Name,@begin,len(@Name)-abs(@temp-@begin)+1)
                insert into #test2(tempcol1) values(@real)

    fetch next from split into @Name

End 
        select  distinct tempcol1 from #test2
Close split
Deallocate split
end
GO
--execution 
exec StringSplitIntoRows 'YourTableName','ColumnName'