SQL Unpivot行并获取订单号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Unpivot行并获取订单号相关的知识,希望对你有一定的参考价值。

我有一排有几个诊断代码。如何将列拆分为行?我尝试了下面的查询来解开,但我不知道如何获得DX_Order这是列号,例如,对于DX01DX_Order = 1。我相信这些NULL由UNPIVOT.处理

当前表

+----+------+-------+-------+------+----+------+
| ID | Name | DX01  | DX02  | DX03 | .. | DX25 |
+----+------+-------+-------+------+----+------+
|  1 | John | 426S3 | G2634 | NULL | .. | NULL |
+----+------+-------+-------+------+----+------+

预期结果

+----+------+----------+-------+
| ID | Name | DX_Order |  DX   |
+----+------+----------+-------+
|  1 | John |        1 | 426S3 |
|  1 | John |        2 | G2634 |
+----+------+----------+-------+

询问

SELECT ID, Name, DX
FROM
(
  SELECT ID, Name, DX01, DX02, DX03...DX25
  FROM dbo.Table
) AS cp
UNPIVOT 
(
  DX FOR DXs IN (DX01, DX02, DX03....DX25)
) AS up;
答案

使用Cross Apply而不是unpivot。与DX0n列一起,您也可以添加订单列

SELECT ID, Name, DX_Order, DX
  FROM dbo.Table
  Cross apply (values (DX01,1),(DX02,2),(DX03,3),...(DX025,25)) cs (DX, DX_Order)
  Where DX is not null

NULL列中有一些DX0n值时,你仍然希望DX_Order是序列然后

SELECT ID,
       NAME,
       DX_Order = Row_number()OVER(ORDER BY DX_Order),
       DX
FROM   (SELECT ID, NAME, DX_Order, DX
        FROM   dbo.Table
               CROSS apply (VALUES (DX01,1),(DX02,2),(DX03,3),..(DX025,25)) cs (DX, DX_Order)
        WHERE  DX IS NOT NULL) a 
另一答案

Pரதீப்解决方案将是我的第一个选择(+1),当然更高效。

但是,以下内容将动态地取消您的数据,而无需指定所有字段,也无需实际使用动态SQL

Declare @YourTable table (ID int,Name varchar(50),DX01 varchar(50),DX02 varchar(50),DX03 varchar(50))
Insert Into @YourTable values
(1,'John','426S3','G2634',null)


Select A.ID
      ,A.Name
      ,DX_Order= C.ColNr
      ,DX = C.Value
 From @YourTable A
 Cross Apply ( values (cast((Select A.* for XML RAW) as xml))) B(XMLData)
 Cross Apply (
                Select ColNr = row_number() over (order by (Select null))
                      ,Field = a.value('local-name(.)','varchar(100)')
                      ,Value = a.value('.','varchar(max)') 
                 From  B.XMLData.nodes('/row')  as C1(n)
                 Cross Apply C1.n.nodes('./@*') as C2(a)
                 Where a.value('local-name(.)','varchar(100)') not in ('ID','Name')
             ) C

返回

ID  Name    DX_Order    DX
1   John    1           426S3
1   John    2           G2634
另一答案

请尝试这样。

SELECT ID,DX_Order,DX
       FROM dbo.TableN
                CROSS apply 
(VALUES ([DX01],1),([DX02],2)) cs (DX,DX_Order)
WHERE DX IS NOT NULL
另一答案

您也可以通过执行动态SQL查询来完成此操作。

询问

declare @sql as varchar(max);

select @sql = stuff((
        select ' union all select [ID], [Name], cast(replace(' 
        + char(39) + [column_name] + char(39) + ', ' + char(39) 
        + 'DX' + char(39) + ',' + char(39) + char(39) + ') as int) as [DX_Order],
        case when [' + [column_name] + '] is not null then [' + [column_name] 
        + '] else null end as [DX] from [your_table_name]'
        from information_schema.columns
        where table_name = 'your_table_name'
        and [column_name] like 'DX%'
        order by ordinal_position
        for xml path(''), type
    ).value('.', 'nvarchar(max)')
    , 1, 11, ''
);

select @sql = 'select t.* from (' + @sql + ') t where t.[DX] is not null;';

exec(@sql); 
另一答案

样本数据创建

IF OBJECT_ID('dbo.temp') iS NOT NULL
DROP TABLE temp
;With cte(ID , Name , DX01  , DX02  , DX03 , DX25 )
AS
( SELECT 1 , 'John' , '426S3' , 'G2634' , NULL,NULL 
)
SELECT * INTO temp FROM cte

使用Dynamic Sql,当我们有n列时,我们可以得到预期的Result

DECLARE @SQlColumn nvarchar(max)
        ,@SQl nvarchar(max)


SELECT @SQlColumn= STUFF((SELECT ', '+Columnname  FROM
                (
            SELECT  '( '+ 'CAST('+QUOTENAME(COLUMN_NAME)+' AS VARCHAR(30)),CAST(REPLACE('''+COLUMN_NAME+''+''','''+'DX'+''',''' +''''+') AS VARCHAR(30)))'  AS Columnname
                FROM INFORMATION_SCHEMA.COLUMNS 
                WHERE TABLE_NAME='temp' AND COLUMN_NAME <>'Id' AND COLUMN_NAME <>'Name'
                )dt FOR XML PATH ('')),1,1,'')

SET @SQl='  SELECT ID
                 ,Name
                 ,DX
                 ,DX_Order FROM temp 
            CROSS APPLY (VALUES '+ @SQlColumn + ') dt (DX,DX_Order) WHERE DX IS NOT NULL'
PRINT @SQl
EXEC(@SQl)

结果

  ID    Name    DX_Order    DX
  ------------------------------
   1    John    01         426S3
   1    John    02         G2634

以上是关于SQL Unpivot行并获取订单号的主要内容,如果未能解决你的问题,请参考以下文章

如何从SQL表中选择特定行并在SQL Server中连接多个表?

使用 UNPIVOT 将代码从 sql 重写为 redshift

如何从 SQL 表中选择特定行并连接 SQL 服务器中的多个表?

从 sql 表中进行复杂的 unpivot

获取具有 stdClass 对象列的行并求和具有相同 id 的元素

SQL Unpivot 多列数据