简单的SQL Pivot查询

时间:2014-12-11 14:30:41

标签: sql-server pivot

我为自己的无知而道歉。我只是不熟悉所有的透视查询,我发现的所有例子看起来都像泥浆一样清晰。我有返回GroupName和ID Numbers的表。

例如:

SELECT GroupName, IDnumber FROM do.Table_1

返回

GroupName                IDnumber
1                          8395
1                          A660
1                          8396
1                          A661
2                          8398
2                          A662
2                          8399

我想要的是更像这样的东西:

GroupName              ID1     ID2   ID3   ID4
1                      8395   A660  8396   A661
2                      8398   A662  8399   NULL

我该怎么做?透视查询?其他一些方法?

我愿意接受建议并感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:3)

是的,你可以使用PIVOT但不是这种形状,你首先要生成一个行号,用它来按照你想要的方式格式化数据。像这样:

WITH Ranked
AS
(
  SELECT GroupName, IDnumber, 
     ROW_NUMBER() OVER(PARTITION BY GroupName ORDER BY GroupName) AS RN
  FROM Table1
)
SELECT GroupName,
  [1] AS ID1, [2] AS ID2, [3] AS ID3, [4] AS ID4
FROM Ranked AS r
PIVOT
(
  MAX(IDnumber)
  FOR RN IN([1], [2], [3], [4])
) AS p;

这会给你:

| GROUPNAME |  ID1 |  ID2 |  ID3 |    ID4 |
|-----------|------|------|------|--------|
|         1 | 8395 | A660 | 8396 |   A661 |
|         2 | 8398 | A662 | 8399 | (null) |

如果您想动态执行此操作而不是在pivot表运算符中手动编写行号,则必须使用动态SQL执行此操作,例如:

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @colnames AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

SELECT @cols = STUFF((SELECT distinct ',' +
                        QUOTENAME(RN)
                       FROM 
               (
                 SELECT ROW_NUMBER() OVER(PARTITION BY GroupName ORDER BY GroupName) AS RN
                 FROM Table1 
               ) AS t
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');

SELECT @colnames = STUFF((SELECT distinct ',' +
                        QUOTENAME(RN) + 'AS' +   
                        QUOTENAME('ID' + CAST(RN AS NVARCHAR(5)))
                       FROM 
               (
                 SELECT ROW_NUMBER() OVER(PARTITION BY GroupName ORDER BY GroupName) AS RN
                 FROM Table1 
               ) AS t
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');

SELECT @query = 'WITH Ranked
                  AS
                  (
                    SELECT GroupName, IDnumber, 
                    ROW_NUMBER() OVER(PARTITION BY GroupName ORDER BY GroupName) AS RN
                    FROM Table1
                  )
                  SELECT GroupName, ' + @colnames +
                  ' FROM Ranked AS r
                  PIVOT
                  (
                    MAX(IDnumber)
                    FOR RN IN(' + @cols + ')' +
                  ') p';

execute(@query);

这应该会给你相同的结果:

| GROUPNAME |  ID1 |  ID2 |  ID3 |    ID4 |
|-----------|------|------|------|--------|
|         1 | 8395 | A660 | 8396 |   A661 |
|         2 | 8398 | A662 | 8399 | (null) |

答案 1 :(得分:0)

您可能需要使用动态旋转,因为Id将是动态的。这是您的样本表

SELECT * INTO #TEMP
FROM 
(
SELECT 1 GroupName,                '8395' IDnumber
  UNION ALL
SELECT 1,                          'A660'
  UNION ALL
SELECT 1,                          '8396'
  UNION ALL
SELECT 1,                          'A661'
  UNION ALL
SELECT 2,                          '8398'
  UNION ALL
SELECT 2,                          'A662'
  UNION ALL
SELECT 2,                          '8399'
)TAB

在每个Groupname上选择行号并插入临时表,以便它可以用于选择旋转列和枢轴内

SELECT *,
'ID' + CAST(ROW_NUMBER() OVER(PARTITION BY GroupName ORDER BY GROUPNAME) AS VARCHAR(10)) IDS
INTO #NEWTABLE
FROM #TEMP

选择数据透视列

DECLARE @cols NVARCHAR (MAX)

SELECT @cols = COALESCE (@cols + ',[' + IDS + ']', 
           '[' + IDS + ']')
           FROM    (SELECT DISTINCT IDS FROM #NEWTABLE) PV  
           ORDER BY IDS

现在动态转动

DECLARE @query NVARCHAR(MAX)
SET @query = '           
              SELECT * FROM 
             (
                 SELECT * FROM #NEWTABLE
             ) x
             PIVOT 
             (
                 MAX(IDnumber)
                 FOR IDS IN (' + @cols + ')
            ) p      

            '     
EXEC SP_EXECUTESQL @query

Click here to view the result(如果加载页面出现错误,请按RUNSQL,它可以正常工作)

<强> RESULT

enter image description here