sql server中的表行到列

Posted

技术标签:

【中文标题】sql server中的表行到列【英文标题】:Table Rows to columns in sql server 【发布时间】:2019-10-28 09:40:39 【问题描述】:

我有一个标题和一个明细表。我希望每一行都重复我的列名。

我的表头有这样的数据

---------------------------------
ID      Product         Status
---------------------------------
1       P1              S1
---------------------------------

而且我的详细信息有这样的数据。

---------------------------------
ID      Spec1   Spec2   Spec3
---------------------------------
1       A       B       C
1       D       E       F
1       G       H       I
---------------------------------

我想要这样的输出,即表格行到列

 ----------------------------------------------------------------------------
 Product   Status   S11   S12  S13  S21  S22  S23  S31  S32  S33
 ----------------------------------------------------------------------------
 P1         S1      A     D    G    B    E    H    C    F    I
 ----------------------------------------------------------------------------

我试过这样..

    SELECT  Hdr.Status,
            Hdr.Product,
            Dtl.Spec1,
            Dtl.Spec2,
            Dtl.Spec3,
            ROW_NUMBER() OVER(ORDER BY Hdr.Root_uuid)   AS  SlNo
    INTO    #Temp
    FROM    TBL_HOME_HEADER_TEST    Hdr
    JOIN    TBL_HOME_Detail_TEST    Dtl
      ON    (Hdr.ID =   Dtl.ID)


     DECLARE    @RowNo      Int     =   1,
                @RowCount   Int     =   (SELECT COUNT(*) FROM #Temp),
                @Field1     Varchar(100),
                @Field2     Varchar(100),
                @Field3     Varchar(100),
                @ProductName    Varchar(100),
                @StatusName     Varchar(100),
                @Query      NVarchar(max) = ''  

      SELECT    DISTINCT @StatusName = Status,@ProductName = Product
      FROM  #Temp

      SET       @Query  =   'SELECT '''+@StatusName+'''AS Status , '''+ 
             @ProductName+''' AS ProductName '  

      WHILE @RowCount   <>  0
      BEGIN

           SELECT   @Field1 =   Spec1,
                    @Field2 =   Spec2,
                    @Field3 =   Spec3
           FROM #Temp
           WHERE    SlNo    =   @RowNo

          SET       @Query  =   @Query + ','''+@Field1+'''AS 
              Filed'+CONVERT(Varchar(10),@RowNo)+'#Name , '''+ @Field2+''' AS 
              Field'+CONVERT(Varchar(10),@RowNo)+'#Name ,'''+@Field3+''' AS 
              Field'+CONVERT(Varchar(10),@RowNo)+'#Name'

       SET  @RowNo      =   @RowNo +1
       SET  @RowCount   =   @RowCount   -   1

 END

 EXECUTE (@Query)

它正在工作,但我想知道,还有其他解决方案吗?

【问题讨论】:

【参考方案1】:

我会使用交叉制表符,而不是“糟糕的”WHILE 来做到这一点:

CREATE TABLE dbo.Header (ID int,
                         Product char(2),
                         Status char(2));
GO

CREATE TABLE dbo.Detail (ID int,
                         Spec1 char(1),
                         Spec2 char(1),
                         Spec3 char(1));
GO

INSERT INTO dbo.Header (ID,
                    Product,
                    Status)
VALUES(1,'P1','S1');
GO

INSERT INTO dbo.Detail (ID,
                    Spec1,
                    Spec2,
                    Spec3)
VALUES(1,'A','B','C'),
      (1,'D','E','F'),
      (1,'G','H','I');
GO

WITH RNs AS(
    SELECT ID,
           Spec1,
           Spec2,
           Spec3,
           ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Spec1,Spec2,Spec3) AS RN
    FROM dbo.Detail)
SELECT H.ID,
       H.Status,
       MAX(CASE R.RN WHEN 1 THEN R.Spec1 END) AS S11,
       MAX(CASE R.RN WHEN 2 THEN R.Spec1 END) AS S12,
       MAX(CASE R.RN WHEN 3 THEN R.Spec1 END) AS S13,
       MAX(CASE R.RN WHEN 1 THEN R.Spec2 END) AS S21,
       MAX(CASE R.RN WHEN 2 THEN R.Spec2 END) AS S22,
       MAX(CASE R.RN WHEN 3 THEN R.Spec2 END) AS S23,
       MAX(CASE R.RN WHEN 1 THEN R.Spec3 END) AS S31,
       MAX(CASE R.RN WHEN 2 THEN R.Spec3 END) AS S32,
       MAX(CASE R.RN WHEN 3 THEN R.Spec3 END) AS S33    
FROM dbo.Header H
     JOIN RNs R ON H.ID = R.ID
GROUP BY H.ID,
         H.Status;


GO

DROP TABLE dbo.Header;
DROP TABLE dbo.Detail;

【讨论】:

感谢您的回答。但如果我有超过 3 条记录,它就不起作用。而且详细表中的记录数也无法预测。 你没有在你的问题中表达这些事实,@ElbyJoy。只有问题是答案,答案才能准确。我建议在这个网站上搜索“[sql-server] Dynamic Pivot”;你会得到 100 条答案。

以上是关于sql server中的表行到列的主要内容,如果未能解决你的问题,请参考以下文章

如何在 sql server 2008 中编写对行到列的查询?

MySQL - 行到列

Sql Server如何使用date数据类型将日期插入到列中

从行到列的 SQL 结果

动态透视行到列-SQL 服务器

SQL Pivot 行到列标题