SQL将行作为列

时间:2012-11-19 13:39:10

标签: sql

  

可能重复:
  SQL Server: column to row

我希望将具有相同日期/时间的数据组织为一行而不是多行。

这是原始数据

  date          time              variable                  value
2012-11-01  18:02:07.0000000    Batch.BasisBatchSize                                                    450.0                         
2012-11-01  18:02:07.0000000    Batch.BatchSize                                                         450.0                         
2012-11-01  18:02:07.0000000    Batch.BatchStartTime                                                                  2012-10-30-11:20:05.786       
2012-11-01  18:02:07.0000000    Batch.RecipeName                                                        MIX-CIP
2012-11-01  18:02:07.0000000    BinID[1]                                                                0.0                           
2012-11-01  18:02:07.0000000    BinID[2]                                                                0.0                           
2012-11-01  18:02:07.0000000    BinID[3]                                                                0.0                           
2012-11-01  18:02:07.0000000    SBinID[4]                                                               0.0                           
2012-11-01  18:02:07.0000000    BinID[5]                                                                0.0                           
2012-11-01  18:02:07.0000000    BinID[6]                                                                0.0                           
2012-11-01  18:02:07.0000000    BinID[7]                                                            0.0                           
2012-11-01  18:02:07.0000000    BinID[8]                                                                0.0                           
2012-11-01  18:02:07.0000000    BinID[9]                                                                0.0                           
2012-11-01  18:02:07.0000000    BinID[10]                                                               0.0                                              
2012-11-01  18:02:07.0000000    Flavour[1].Weight                                                           0.0                           
2012-11-01  18:02:07.0000000    Flavour[2].Weight                                                       0.0                           
2012-11-01  18:02:07.0000000    Flavour[3].Weight                                                       0.0                           
2012-11-01  18:02:07.0000000    Flavour[4].Weight                                                       0.0                           
2012-11-01  18:02:07.0000000    Flavour[5].Weight                                                       0.0                           

我希望它看起来像这样:

date        time                Batch.BasisBatchSize   Batch.BatchSize  
2012-11-01  18:02:07.0000000             450.0                    

请帮助:)

1 个答案:

答案 0 :(得分:2)

在SQL Server中,您可以使用PIVOT函数执行此数据转换。有几种方法可以执行此操作。

静态版本是您对所有值进行硬编码的地方:

select *
from
(
  select date, time, variable, value
  from yourtable
) src
pivot
(
  max(value)
  for variable in ([Batch.BasisBatchSize], [Batch.BatchSize])  -- other column will go here
) piv;

请参阅SQL Fiddle with Demo

在动态版本中,列的列表是在运行时收集的:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(variable) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT date, time, ' + @cols + ' from 
             (
                select date, time, variable, value
                from yourtable
            ) x
            pivot 
            (
                max(value)
                for variable in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demo

如果您无权访问PIVOT函数,则可以使用带有CASE语句的聚合函数:

select date,
  time,
  max(case when variable = 'Batch.BasisBatchSize' then value end) as [Batch.BasisBatchSize],
  max(case when variable = 'Batch.BatchSize' then value end) as [Batch.BatchSize],
  max(case when variable = 'Batch.BatchStartTime' then value end) as [Batch.BatchStartTime],
  max(case when variable = 'Batch.RecipeName' then value end) as [Batch.RecipeName]
from yourtable
group by date, time

请参阅SQL Fiddle with Demo

所有版本都会返回相同的结果:

|                            DATE |             TIME | BATCH.BASISBATCHSIZE | BATCH.BATCHSIZE |    BATCH.BATCHSTARTTIME | BATCH.RECIPENAME |
--------------------------------------------------------------------------------------------------------------------------------------------
| November, 01 2012 00:00:00+0000 | 18:02:07.0000000 |                450.0 |           450.0 | 2012-10-30-11:20:05.786 |          MIX-CIP |