0

可能重复:
SQL Server:列到行

我想获取具有相同日期/时间的数据以组织为一行而不是多行。

这是原始数据

  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                    

请帮忙 :)

4

1 回答 1

2

In SQL Server you can use the PIVOT function to perform this data transformation. There are a few ways that this can be performed.

Static version is where you hard-code all of the values:

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;

See SQL Fiddle with Demo

In a dynamic version the list of columns is gathered at run-time:

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)

See SQL Fiddle with Demo

If you do not have access to a PIVOT function then you can use an aggregate function with a CASE statement:

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

See SQL Fiddle with Demo

All versions will return the same result:

|                            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 |
于 2012-11-19T14:28:01.527 回答