我的数据集看起来像这样:
Gender | Age | Name
Male | 30 | Bill
Female | 27 | Jenny
Female | 27 | Debby
Male | 44 | Frank
我试图将其显示为特殊格式的HTML代码:
<ul>
<li>Male
<ul>
<li>30
<ul>
<li>Bill</li>
</ul>
</li>
<li>44
<ul>
<li>Frank</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>Female
<ul>
<li>27
<ul>
<li>Jenny</li>
<li>Debby</li>
</ul>
</li>
</ul>
</li>
</ul>
我尝试使用FOR XML
但是没有给出我想要的结果。它没有删除返回的多个Gender
和Age
字段。正如你在这个HTML中看到的那样,它正在复合它,只在末端节点上提供重复。
Aaron Bertrand在这里提供了一个很好的方法Return Select Statement as formatted HTML,它似乎在SQL 2008中运行得很好,但我在寻找2005年也能运行的东西,除了像+=
运算符这样的小东西之外设置默认的DECLARE值,只是在2005年没有显示。
在SQL Server 2005中如何实现这样的目标?
答案 0 :(得分:5)
declare @T table
(
Gender varchar(10),
Age int,
Name varchar(10)
)
insert into @T values ('Male', 30, 'Bill')
insert into @T values ('Female', 27, 'Jenny')
insert into @T values ('Female', 27, 'Debby')
insert into @T values ('Male', 44, 'Frank')
select Gender as 'li',
(select T2.Age as 'li',
(select T3.Name as 'li'
from @T as T3
where T2.Age = T3.Age and
T1.Gender = T3.Gender
for xml path(''), root('ul'), type) as 'li'
from @T as T2
where T1.Gender = T2.Gender
group by T2.Age
for xml path(''), root('ul'), type) as 'li'
from @T as T1
group by Gender
for xml path('ul')
答案 1 :(得分:4)
以下是SQL Server 2005的代码,它只需要为SQL Server 2008专门请求的小调整from the answer I gave yesterday:
DECLARE @x TABLE(Gender VARCHAR(6), Age INT, Name VARCHAR(32));
INSERT @x SELECT 'Male', 30, 'Bill' UNION ALL SELECT 'Female', 27, 'Jenny'
UNION ALL SELECT 'Female', 27, 'Debby' UNION ALL SELECT 'Male', 44, 'Frank';
DECLARE @html NVARCHAR(MAX);
SET @html = N'';
;WITH x AS ( SELECT x.Age, x.Gender, x.Name,
dr = DENSE_RANK() OVER (PARTITION BY x.Gender ORDER BY x.Age),
gn = ROW_NUMBER() OVER (PARTITION BY x.Gender ORDER BY x.Age),
rn = ROW_NUMBER() OVER (ORDER BY x.Gender DESC, x.Age)
FROM @x AS x ) SELECT @html = @html +
CHAR(13) + CHAR(10) + CASE WHEN c1.gn = 1 THEN
CASE WHEN c1.rn > 1 THEN '</li></ul></li></ul>' ELSE '' END + '<ul><li>'
+ c1.Gender ELSE '' END + CHAR(13) + CHAR(10) + CHAR(9)
+ CASE WHEN c1.gn = 1 OR c1.Age <> c3.Age THEN
CASE WHEN c1.gn > 1 THEN '</li>' ELSE '<ul>' END + '<li>'
+ CONVERT(VARCHAR(32), c1.Age) ELSE '' END + CHAR(13) + CHAR(10) + CHAR(9)
+ CHAR(9) + CASE WHEN (c1.gn = 1 OR c1.Age <> c3.Age) THEN '<ul>' ELSE '' END
+ '<li>' + c1.Name + '</li>' + CASE WHEN c1.Age <> c2.Age OR c1.dr <> c2.dr
THEN '</ul>' ELSE '' END
FROM x AS c1
LEFT OUTER JOIN x AS c2
ON c1.rn = c2.rn - 1
LEFT OUTER JOIN x AS c3
ON c1.rn = c3.rn + 1
ORDER BY c1.Gender DESC, c1.Age;
SELECT @html = @html + '</ul></li></ul></li></ul>';
PRINT @html; -- note you will need to deal with this
-- in another way if the string is large
以下证明它有效:
答案 2 :(得分:0)
SET ANSI_WARNINGS ON;
DECLARE @Test TABLE
(
Gender VARCHAR(10) NOT NULL,
Age INT NOT NULL,
Name VARCHAR(50) NOT NULL
);
INSERT @Test (Gender, Age, Name)
SELECT 'Male' ,30 ,'Bill'
UNION ALL
SELECT 'Female',27 ,'Jenny'
UNION ALL
SELECT 'Female',27 ,'Debby'
UNION ALL
SELECT 'Male' ,44 ,'Frank';
DECLARE @x XML;
SET @x =
(
SELECT *
FROM @Test t
FOR XML RAW, ROOT
);
SELECT @x AS [Source];
SELECT @x.query('
for $g in distinct-values(//root/row/@Gender) (: or for $g in ("Male", "Female") :)
return
<ul>
<li>
{data($g)}
<ul>
{
for $a in distinct-values(//root/row[@Gender=$g]/@Age)
return <li>{data($a)}
<ul>
{
for $n in //root/row
where $n/@Gender=$g and $n/@Age = $a
return <li>{data($n/@Name)}</li>
}</ul></li>
}
</ul>
</li>
</ul>
') AS Result;
Refences:SQL Server XQuery