2017-05-19 1 views
1

In einer SQL Server 2012-Datenbank soll ich zählen, wie oft jede "in Dosen" Nachricht für Grundschüler im letzten Schuljahr und der aktuellen verwendet wird Schuljahr.SQL Server 2012 split mehrere Werte in einem Varchar (1200)

Im Moment habe ich den folgenden T-SQL, die Art funktioniert:

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 

Das Problem ist die GS.Comments Spalt als varchar(1200) definiert. Es kann eine Nachricht in der Spalte geben und/oder es kann viele Nachrichten in dieser Spalte geben. Jede Nachricht endet mit einem Punkt und zwischen jeder Nachricht befindet sich ein Leerzeichen.

Ein Beispiel für mehrere Nachrichten in der einen GS.Comments Spalte würde wie folgt aussehen:

This student seems to enjoy school. 
:

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. 

Ein Beispiel, wenn man Nachrichten in der eine GS.Comments Spalte wie folgt aussehen würde

So würde mir die T-SQL-Logik zeigen, die ich verwenden kann, wenn die GS.Comments Spalte mehrere Nachrichten und/oder nur eine Nachricht enthält, so dass ich zählen kann, wie oft jede eindeutige Nachricht verwendet wurde?

Antwort

0
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,@[email protected])     
      set @[email protected]+1 
      insert into #test2(tempcol1) values(@result)  
     end 

     set @real = SUBSTRING(@Name,@begin,len(@Name)-abs(@[email protected])+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'