実際、私は SQL XML パスの初心者なので、プロになります。シナリオを手に入れました...次のような CTE 関数があります。
Data Chars NumberOfOccurance
12 1 1 appears (1 ) times
12 2 2 appears (1 ) times
xx x x appears (2 ) times
CTE関数は次のとおりです。
;with cte as
(
select Data , SUBSTRING(Data,1,1) as Chars,1 as startpos from @t
union all
select Data, SUBSTRING(Data, startpos+1,1) as char,startpos+1 from cte where startpos+1<=LEN(data)
)
select Data,Chars,Cast(Chars as varchar(1)) + ' appears (' + cast(COUNT(*) as varchar(5))+ ' ) times' as 'NumberOfOccurance' from cte
group by data, chars
実際、私は自分の答えをこれにしたいだけです:
data Number_of_occurances
12 1 appears (1) times 2 appears (1) times
xx x appears (2) times
私はこれを試しました:
; With Ctea as
(
select Data , SUBSTRING(Data,1,1) as Chars,1 as startpos from @t
union all
select Data, SUBSTRING(Data, startpos+1,1) as char,startpos+1 from ctea where startpos+1<=LEN(data)
)
select Data,Chars,REPLACE((SELECT (Cast(Chars as varchar(1)) + ' appears (' + cast(COUNT(*) as varchar(5))+ ' ) times') AS [data()] FROM Ctea t2 WHERE t2.Data = t1.data FOR XML PATH('')), ' ', ' ;') As Number_of_occurances from ctea as t1
group by t1.data, t1.Chars
それは言います:
Column 'Ctea.Chars' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
一時テーブルを使用して正確な答えを得たが、それができない場合 CTE
誰でも私の結果を作ることができますか?