I have a query that I am running for some search results on my page. The stored procedure looks like this:
BEGIN
SET NOCOUNT ON;
BEGIN
SELECT *
FROM (SELECT id,
data,
dataType,
dataLocation,
tag,
whoAdded,
whenAdded,
notes,
ROW_NUMBER() OVER (PARTITION BY data ORDER BY whenAdded DESC) AS rn
FROM Tags_Accounts
WHERE tag IN (SELECT *
FROM dbo.splitstring (@tags))) AS a
WHERE rn = 1
FOR XML PATH ('results'), TYPE, ELEMENTS, ROOT ('root');
END
END
This will go through my records that looks like the following:
Now, If I were to search for the tag stuff, paypal it should return 2 results as the partition is on the data column.
The XML output is as follows for the results:
<root>
<results>
<id>43</id>
<data>123333</data>
<dataType>1</dataType>
<dataLocation>AF</dataLocation>
<tag>paypal</tag>
<whoAdded>chussey</whoAdded>
<whenAdded>2013-11-22T11:01:50.117</whenAdded>
<rn>1</rn>
</results>
<results>
<id>41</id>
<data>12345</data>
<dataType>1</dataType>
<dataLocation>AF</dataLocation>
<tag>paypal</tag>
<whoAdded>chussey</whoAdded>
<whenAdded>2013-11-22T10:59:39.277</whenAdded>
<rn>1</rn>
</results>
<results>
<id>50</id>
<data>RGG</data>
<dataType>2</dataType>
<dataLocation>AF</dataLocation>
<tag>stuff</tag>
<whoAdded>chussey</whoAdded>
<whenAdded>2013-11-22T22:25:41.393</whenAdded>
<rn>1</rn>
</results>
</root>
It included the 2 results for PayPal as the data was different in the data col. It returned 1 record for stuff as the data was already found matching in the results for the tag PayPal.
My question: is there any way to do exactly what it's doing now but in the <Tag></Tag>
include all of the tags that match the data in the data column?
For example, because the tag stuff
was found with the other tag Paypal
it would include them together.
The goal out of this is to find any data that contains the tag searched. If its found and there are more than 1 tag for that piece of data, it will include all the tags with it.
<results>
<id>50</id>
<data>RGG</data>
<dataType>2</dataType>
<dataLocation>AF</dataLocation>
<tag>stuff, testing</tag>
<whoAdded>chussey</whoAdded>
<whenAdded>2013-11-22T22:25:41.393</whenAdded>
<rn>1</rn>
</results>
I think this is doing what you want:
WITH OrderedTags
AS
(
SELECT id,
data,
dataType,
dataLocation,
CAST(tag as NVARCHAR(max)) as tag,
whoAdded,
whenAdded,
notes,
ROW_NUMBER() OVER (PARTITION BY data ORDER BY tag, id) AS rn
FROM Tags_Accounts T
)
,RecurseTags (data, tags, rn)
AS
(
--Anchor
SELECT data,
tag as tags,
rn
FROM OrderedTags T
WHERE rn = 1
UNION ALL
--Recurse
SELECT R.data,
tags + N', ' + tag,
T.rn
FROM RecurseTags R
JOIN OrderedTags T
ON T.data = R.data
AND T.rn = R.rn + 1
)
,CommaSeparatedTags(data, tags)
AS
(
SELECT data, tags
FROM
(
SELECT data,
tags,
ROW_NUMBER() OVER (PARTITION BY data ORDER BY rn DESC) AS rn
FROM RecurseTags
) T
WHERE rn = 1
)
,RequiredRows
AS
(
SELECT * FROM(
SELECT A.id,
A.data,
A.dataType,
A.dataLocation,
C.tags as tag,
A.whoAdded,
A.whenAdded,
A.notes,
ROW_NUMBER() OVER (PARTITION BY A.data ORDER BY A.whenAdded DESC) AS rn
FROM OrderedTags A
JOIN dbo.splitstring (@tags) T
ON T.Value = A.tag
JOIN CommaSeparatedTags C
ON C.data = A.data) A
WHERE rn = 1
)
SELECT * FROM RequiredRows
FOR XML PATH ('results'), TYPE, ELEMENTS, ROOT ('root')
Basically, it orders the tags by data, generates a consolidated table, and then joins your original query to that...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.