0

我正在尝试创建一个动态枢轴代码,其中一些列是枢轴的,而另一些则不是。目前,每个父子组合我有一行。我想要一个表格,其中每个父母都是一行,包含 child1、child2 等的列。有些父母有多个孩子,所以每个父母的列数(孩子)是动态的。我已经有一个按生日对孩子排序的列,并且我正在将这个“childnumber”添加到子列中。

这是我开始的示例表: http ://sqlfiddle.com/#!18/990f6/2

+===============+===========+-=============+=============+================+=============+==============+
|    parent     | parentage | parentgender |  childname  | childbirthdate | childgender | childnumber  |
+===============+===========+==============+=============+================+=============+==============+
| John Smith    |        32 | M            | Jane Smith  | 2005-05-21     | F           |            1 |
| John Smith    |        32 | M            | Billy Smith | 2010-01-01     | M           |            2 |
| Katherine Doe |        40 | F            | Drew Fine   | 2015-08-09     | M           |            1 |
| Paula Lee     |        28 | F            | Peter Lee   | 2009-12-30     | M           |            1 |
| Paula Lee     |        28 | F            | Tim Lee     | 2013-10-15     | M           |            2 |
| Paula Lee     |        28 | F            | Andrew Lee  | 2014-06-27     | M           |            3 |
+---------------+-----------+--------------+-------------+----------------+-------------+--------------+                          

最终结果应该是:

+===============+===========+==============+============+=================+==============+=============+=================+==============+============+=================+===============+
|    parent     | parentage | parentgender | childname1 | childbirthdate1 | childgender1 | childname2  | childbirthdate2 | childgender2 | childname3 | childbirthdate3 | childgender3  |
+===============+===========+==============+============+=================+==============+=============+=================+==============+============+=================+===============+
| John Smith    |        32 | M            | Jane Smith | 2005-05-21      | F            | Billy Smith | 2010-01-01      | M            | null       | null            | null          |
| Katherine Doe |        40 | F            | Drew Fine  | 2015-08-09      | M            | null        | null            | null         | null       | null            | null          |
| Paula Lee     |        28 | F            | Peter Lee  | 2009-12-30      | M            | Tim Lee     | 2013-10-15      | M            | Andrew Lee | 2014-06-27      | M             |
+---------------+-----------+--------------+------------+-----------------+--------------+-------------+-----------------+--------------+------------+-----------------+---------------+

我的 SQL Server 代码尝试:

IF OBJECT_ID('finaltable', 'U') IS NOT NULL DROP TABLE finaltable

DECLARE @columns AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)

SELECT @columns = STUFF((SELECT DISTINCT ',' + QUOTENAME(col + '_' + CAST(childnumber as varchar(50)))
FROM
  families
FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'), 1, 1,'')

SET @query = 'SELECT parent, ' + @columns + '  
             FROM (
                SELECT parent, parentage, parentgender, num = col+''_''+ CAST(childnumber as varchar(50)) 
                FROM (
                    SELECT parent, childnumber, childname, childbirthdate, childgender
                    FROM families
                    ) AS x
            ) AS source
            PIVOT
            (
                MAX(childnumber)
                FOR num in (' + @columns + ')
            ) AS pvt '

execute(@query);

我无法执行此查询,并且我不确定我的问题是否与定义“col”有关,或者是否存在其他问题,例如需要先取消透视。任何帮助是极大的赞赏。

4

0 回答 0