我有2个名为' Categories'和CSTagContent'如下所示,数据......
表1:'类别'
CategoryID PostID <----Categories Table
1148 581771
1183 581771
1184 581771
表2:&#39; CSTagContent&#39;
ID TagContent StartDate EndDate CategoryID TagTitle <---CSTagContent Table
1 <blockquote><p> 2014-11-08 2014-11-14 1148
<a href="abc.com">
</p></blockquote>
2 <blockquote><p> 2014-11-25 2014-12-05 1183 <h1>Aging Title</h1>
<a href="abc.com">
</p></blockquote>
3 <blockquote><p> 2014-11-25 2014-11-27 1184 <h1>Allergies Title</h1>
<a href="abc.com">
</p></blockquote>
我的查询:
SELECT
st.TagContent, st.TagTitle
FROM
Categories cpc
INNER JOIN
CSTagContent st ON st.CategoryID = cpc.CategoryID
WHERE
cpc.PostID = 581771
AND st.TagContent IS NOT NULL
AND st.TagContent <> ''
AND GETDATE() > st.StartDate
AND GETDATE() < DATEADD(dd, 1, st.EndDate)
当前输出:
TagContent TagTitle
<blockquote><p>
<a href="abc.com"> <h1>Aging Title</h1>
</p></blockquote>
<blockquote><p> <h1>Allergies Title</h1>
<a href="abc.com">
</p></blockquote>
在上面的输出TagContent
中,两行都有相同的值,所以我希望它是不同的,TagTitle
应该追加或合并为&#39;&amp;&#39;与其他行/行如下所示...
预期输出:
TagContent TagTitle
<blockquote><p>
<a href="abc.com"> <h1>Aging Title</h1>&<h1>Allergies Title</h1>
</p></blockquote>
提前致谢..!
答案 0 :(得分:1)
示例表格
SELECT * INTO Categories
FROM
(
SELECT 1148 CategoryId, 581771 PostId
UNION ALL
SELECT 1183 CategoryId, 581771 PostId
UNION ALL
SELECT 1184 CategoryId, 581771 PostId
)TAB
SELECT * INTO TagContent
FROM
(
SELECT 1 [Id], '<blockquote><p><a href="abc.com"></p></blockquote>' TagContent , '2014-11-08' StartDate, '2014-11-14' EndDate, 1148 CategoryID, NULL TagTitle
UNION ALL
SELECT 2, '<blockquote><p><a href="abc.com"></p></blockquote>', '2014-11-25', '2014-12-05', 1183, '<h1>Aging Title</h1>'
UNION ALL
SELECT 3, '<blockquote><p><a href="abc.com"></p></blockquote>', '2014-11-25', '2014-11-27', 1184, '<h1>Allergies Title</h1>'
)TAB
现在,我们将TagTitle
转换为同一TagContent
的Ambersand分隔值。由于使用了XML格式,我们需要替换>, < and
&amp; amp to <, > and &
。
<强> QUERY 强>
SELECT DISTINCT TagContent,STUFF(REPLACE(REPLACE(REPLACE(REPLACE(SUBSTRING(
(SELECT '&' + TagTitle
FROM TagContent T2
WHERE ST.TagContent=T2.TagContent
FOR XML PATH('')),2,200000),'<','<'),'>','>'),'&','&'),'amp;',''),1,'') TagTitle
FROM Categories CPC
JOIN TagContent ST ON CPC.CategoryId=ST.CategoryId