-1

我有一个看起来像这样的数据集:

   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,但这并没有给出我想要的结果。它没有删除返回的多个GenderAge字段。正如您在此 HTML 中看到的那样,它将所有内容组合在一起,并且仅在结束节点处给出重复项。

在 SQL Server 中如何实现这样的功能?

4

2 回答 2

5

这是手动制定 HTML 的一种非常丑陋的方式。这不属于 SQL Server 是有充分理由的。我确信一些 XML 专家会出现并用一种更直接的方法让我感到尴尬(我使用过Simon Sabin 的解决方案,但无法将其转换为您的要求),但现在:

DECLARE @x TABLE(Gender VARCHAR(6), Age INT, Name VARCHAR(32));

INSERT @x VALUES  ('Male',   30, 'Bill'),  ('Female', 27, 'Jenny'),
                  ('Female', 27, 'Debby'), ('Male',   44, 'Frank');

DECLARE @html NVARCHAR(MAX) = 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 +=
    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 += '</ul></li></ul></li></ul>';

PRINT @html; -- note you will need to deal with this 
             -- in another way if the string is large

结果 - 不是您在空白方面所要求的,而是相同的 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>

编辑对于更清洁的解决方案,以及大量的戏剧性和很好的演示为什么@ZeeTee 是 StackOverflow 上最烦人的用户,请参阅 Mikael 对后续问题的解决方案:

将 Select 语句返回为格式化的 HTML (SQL 2005)

于 2012-04-24T01:04:37.230 回答
0

注意:我已替换for $g in ("Male", "Female")for $g in distinct-values(//root /row/@Gender) .

注意:2我已经删除了每个性别的重复年龄(此处演示)。

此外,这可以使用 XQuery 来完成:

DECLARE @x XML = 
(
    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;

结果:

Source
----------------------------------------------------
<root>
  <row Gender="Male" Age="30" Name="Bill" />
  <row Gender="Female" Age="27" Name="Jenny" />
  <row Gender="Female" Age="27" Name="Debby" />
  <row Gender="Male" Age="44" Name="Frank" />
</root>

Result
----------------------------------------------------
<ul><li>Female<ul><li>27<ul><li>Jenny</li><li>Debby</li></ul></li></ul></li></ul>
<ul><li>Male<ul><li>30<ul><li>Bill</li></ul></li><li>44<ul><li>Frank</li></ul></li></ul></li></ul>
于 2012-04-24T06:34:11.440 回答