This challenge involves counting the number of occurrences of HTML tags in a given list of strings. You can assume that there will be only valid HTML tags in the input strings. The output should display tags in ascending order.
ID HtmlText
-- ------------------------------------------------------------------------
1 <html><body><font>This is challenge #18</Font><font></Font></Body></Html>
ID TagNamesOccurance
-- --------------------------------------------------------------
1 Body(Found: 1 time), Font(Found: 2 times), Html(Found: 1 time)
- Rules
- ID should be sorted in ascending order.
- The program should run in SQL SERVER 2005 and above.
- The output should be in the same way as it has been shown. Column names should be exactly the same and the result must be sorted in Ascending order of ID.
DECLARE @t TABLE(ID INT IDENTITY, HtmlText VARCHAR(Max))
INSERT INTO @t(HtmlText)
SELECT 'This is challenge #18'
;
WITH CTE AS
(
SELECT ID , HtmlText ,
CHARINDEX('<',HtmlText) AS PosStart ,
CHARINDEX('>',HtmlText) AS PosEnd
FROM @t
UNION ALL
SELECT ID , HTMLText ,
CHARINDEX('<',HtmlText,PosEnd + 1) ,
CHARINDEX('>',HtmlText,PosEnd + 1)
FROM CTE
WHERE PosStart > 0
AND PosEnd > 0
)
SELECT
ID ,
'Body(Found: ' + CAST(Body AS varchar) + ') , ' +
'Font(Found: ' + CAST(Font AS varchar) + ') , ' +
'Html(Found: ' + CAST(Html AS varchar) + ')' AS TagNamesOccurance
FROM
(
SELECT
T.ID ,
T.String ,
CAST
(
COUNT(T.String) AS varchar(2)
) +
CASE
WHEN COUNT(T.String) > 1 THEN ' times'
ELSE ' time'
END AS Data
FROM
(
SELECT
ID ,
SUBSTRING
(
HtmlText,
PosStart + 1 ,
PosEnd - (PosStart + 1)
) AS String
FROM CTE
WHERE PosStart > 0
AND PosEnd > 0
) AS T
GROUP BY T.ID , T.String
) AS P
PIVOT
(
MAX(Data) FOR String IN ([Body],[Font],[Html])
) AS PV
ORDER BY ID
沒有留言:
張貼留言