2

其實我米begineer到SQL XML路徑,以便使我的專業,有一個場景...... 我有一個CTE功能結果SQL XML路徑轉換結果錯誤

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

誰能讓我的結果?

回答

0

您可以使用FOR XML PATH這樣的級聯

;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) 
    ), CTE2 AS 
    (
    select Data,Chars,Cast(Chars as varchar(1)) + ' appears (' + cast(COUNT(*) as varchar(5))+ ') times' as 'NumberOfOccurance' from cte 
    group by data, chars 
    ) 
    SELECT Data,(SELECT NumberOfOccurance + ' ' FROM CTE2 c2 WHERE c2.Data = C1.Data FOR XML PATH(''),type).value('.','VARCHAR(MAX)') as Number_of_occurances 
    FROM CTE2 C1 
    GROUP BY Data 
2

的問題無關,與你的CTE。實際上,它主要表現在以下子查詢:

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('') 

注意你是如何在這裏使用聚合COUNT(*)以及列Chars。您需要按至少Chars這裏:

SELECT (Cast(Chars as varchar(1)) + 
    ' appears (' + 
    cast(COUNT(*) as varchar(5)) + 
    ') times') AS [data()] 
FROM Ctea t2 
WHERE t2.Data = t1.data 
GROUP BY t2.Chars 
FOR XML PATH('') 

此外,你t1.Chars外部查詢選擇或組,因爲它會導致每個字符數的值一行:

 
data chars Number_of_occurances 
12 1  1 appears (1) times 2 appears (1) times 
12 2  1 appears (1) times 2 appears (1) times 
xx x  x appears (2) times 

最後,你應該最有可能被使用STUFF function,而不是REPLACE,因爲你正試圖創造一個空間分隔的列表(「1出現(1)次2次出現(1)次」),而不是取代所有的空間帶有空格和分號的字符(「1;出現;(1);次數; 2;出現;(1);次數」)。

所以你最終的查詢應該是:

; 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, 
    STUFF((SELECT cast(' ' as varchar(max)) + (Cast(Chars as varchar(1)) + ' appears (' + cast(COUNT(*) as varchar(5))+ ') times') AS [data()] 
      FROM Ctea t2 
      WHERE t2.Data = t1.data 
      GROUP BY t2.Chars 
      FOR XML PATH('')), 1, 1, '') As Number_of_occurances 
from ctea as t1 
group by t1.data