2017-05-19 7 views
1

SQL Server 2012データベースでは、最後の学年の小学生に現在の「缶詰め」メッセージが使用された回数をカウントし、現在の学年。問題がGS.Comments列がvarchar(1200)のように定義されている複数の値を1つのvarchar(1200)に分割する

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 

今、私は、作品の種類、次のT-SQLを持っています。列にメッセージが1つあります。この列には多数のメッセージがあります。各メッセージはピリオドで終わり、各メッセージの間にスペースがあります。

1つの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. 

1つのメッセージは、カラムは次のようになります。1 GS.Commentsである場合の例:

This student seems to enjoy school. 

したがって、GS.Comments列に複数のメッセージや1つのメッセージが含まれているため、各固有のメッセージが使用された回数を数えることができるときに使用できるT-SQLロジックが表示されます。

答えて

0

次のリンクを使用して期間に列を分割できます。新しく形成された列の単純なグループによって、それを数えることができます。

Splitting delimited values in a SQL column into multiple rows

+0

あなたは私のsplit関数を使用して、より具体的な例を与えることができますか? – user1816979

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' 
関連する問題