将多行转换为列编辑

时间:2014-08-28 09:03:08

标签: sql sql-server multiple-columns rows

行。我编辑我的问题。这是我的剧本:

 CREATE TABLE #Table (
    Data Date, 
    Max_Temp Real, 
    Min_Temp Real,
    Sr_Temp Real, 
    Sr_Temp_work Real)

    SET NOCOUNT ON
    DECLARE @StartTime Time
    DECLARE @EndTime Time 
    DECLARE @StartTime1 Time
    DECLARE @EndTime1 Time 
    DECLARE @data_start Date
    DECLARE @data_stop Date
    DECLARE @Data Date
    DECLARE @Data_stop_while Date
    DECLARE @Max_Temp Real
    DECLARE @Min_Temp Real
    DECLARE @Sr_Temp Real
    DECLARE @Sr_Temp_work Real


    SET @data_start = '20140713'
    SET @data_stop = '20140719'
    SET @StartTime = '00:00:00.000'
    SET @EndTime = '23:59:59.998'
    SET @StartTime1 = '08:00:00.000' 
    SET @EndTime1 = '16:30:00.000'
    Set NOCOUNT OFF

    SELECT @Data = @data_start

    SELECT @Data_stop_while = DATEADD(day,1,@data_stop)

   WHILE (@Data_stop_while<>@Data)
    BEGIN

   SELECT @Max_Temp = MAX(Value), @Min_Temp = MIN(Value), @Sr_Temp = AVG(Value) FROM INSQL.Runtime.dbo.History WHERE TagName IN ('VariableName')
    and wwRetrievalMode = 'Cyclic'
    AND DateTime >= (@Data + cast(@StartTime as datetime))
    AND DateTime <= (@Data + cast(@EndTime as datetime))


    SELECT @Sr_Temp_work = AVG(Value) FROM INSQL.Runtime.dbo.History WHERE TagName IN ('VariableName')
    and wwRetrievalMode = 'Cyclic'
    AND DateTime >= (@Data + cast(@StartTime1 as datetime))
    AND DateTime <= (@Data + cast(@EndTime1 as datetime))


    INSERT INTO #Table(Data, Max_Temp, Min_Temp, Sr_Temp, Sr_Temp_work) VALUES (@Data, @Max_Temp, @Min_Temp, @Sr_Temp, @Sr_Temp_work)

    SELECT @Data = DATEADD(day,1,@Data)

   END

    SELECT Data, Max_Temp, Min_Temp, Sr_Temp, Sr_Temp_work FROM #Table GO

当我设置一个特定的时间间隔时,我有这些表,现在我需要转换这个表:

enter image description here

我使用了pivot,unpivot以及任何与论坛的链接,我无法做到......

2 个答案:

答案 0 :(得分:1)

它不是迄今为止最优雅的解决方案,但使用一些动态SQL,您应该能够复制逻辑。

DECLARE @temp TABLE (
    Data DATE,
    Max_temp FLOAT,
    Min_Temp FLOAT,
    Sr_Temp FLOAT, 
    Sr_Temp_work FLOAT
)

INSERT INTO @temp
select
*
from (values 
    ('2014-07-13',25.8,25.6,25.701,25.668),
    ('2014-07-14',26.8,26.6,26.701,26.668),
    ('2014-07-15',27.8,27.6,27.701,27.668),
    ('2014-07-16',28.8,28.6,28.701,28.668))T (Data,Max_temp,Min_Temp,Sr_Temp, Sr_Temp_work)
SELECT
        Data, 
        Max_temp,
        Min_Temp,
        Sr_Temp,
        Sr_Temp_work
FROM @temp


SELECT 
    'Max_temp' Temperature, 
    MAX([2014-07-13])[2014-07-13], 
    MAX([2014-07-14])[2014-07-14], 
    MAX([2014-07-15])[2014-07-15], 
    MAX([2014-07-16])[2014-07-16]
FROM
(
    SELECT
        Data, 
        Max_temp,
        Min_Temp,
        Sr_Temp,
        Sr_Temp_work
    FROM @temp
) SRC
PIVOT (
    MAX(Max_temp)
    FOR Data IN ([2014-07-13],[2014-07-14],[2014-07-15],[2014-07-16])
) AS pvt
UNION ALL 
SELECT 
    'Min_Temp' Temperature, 
    MAX([2014-07-13])[2014-07-13], 
    MAX([2014-07-14])[2014-07-14], 
    MAX([2014-07-15])[2014-07-15], 
    MAX([2014-07-16])[2014-07-16]
FROM
(
    SELECT
        Data, 
        Max_temp,
        Min_Temp,
        Sr_Temp,
        Sr_Temp_work
    FROM @temp
) SRC
PIVOT (
    MAX(Min_Temp)
    FOR Data IN ([2014-07-13],[2014-07-14],[2014-07-15],[2014-07-16])
) AS pvt
UNION ALL 
SELECT 
    'Sr_Temp' Temperature, 
    MAX([2014-07-13])[2014-07-13], 
    MAX([2014-07-14])[2014-07-14], 
    MAX([2014-07-15])[2014-07-15], 
    MAX([2014-07-16])[2014-07-16]
FROM
(
    SELECT
        Data, 
        Max_temp,
        Min_Temp,
        Sr_Temp,
        Sr_Temp_work
    FROM @temp
) SRC
PIVOT (
    MAX(Sr_Temp)
    FOR Data IN ([2014-07-13],[2014-07-14],[2014-07-15],[2014-07-16])
) AS pvt
UNION ALL 
SELECT 
    'Sr_Temp_work' Temperature, 
    MAX([2014-07-13])[2014-07-13], 
    MAX([2014-07-14])[2014-07-14], 
    MAX([2014-07-15])[2014-07-15], 
    MAX([2014-07-16])[2014-07-16]
FROM
(
    SELECT
        Data, 
        Max_temp,
        Min_Temp,
        Sr_Temp,
        Sr_Temp_work
    FROM @temp
) SRC
PIVOT (
    MAX(Sr_Temp_work)
    FOR Data IN ([2014-07-13],[2014-07-14],[2014-07-15],[2014-07-16])
) AS pvt

在不同的注意!

不要逐行使用..

DECLARE
    @data_start Date = '2014-07-13',
    @data_stop Date = '2014-07-19'

SELECT
    [DateTime] Data
    MAX(Value) Max_temp,
    MIX(Value) Min_temp,
    AVG(Value) Sr_Temp
    AVG(case when CONVERT(TIME(7),DateTime) between '08:00:00.000' and '16:30:00.000' then Value else NULL end) Sr_Temp_work
FROM INSQL.Runtime.dbo.History 
WHERE 
    TagName IN ('VariableName') and 
    wwRetrievalMode = 'Cyclic' AND 
    CONVERT(DATE, Datetime) between @data_start and @data_stop

答案 1 :(得分:1)

实际上你需要使用动态数据透视并一起打开。检查以下查询。使用以下代码代替过程中的最后一个选择查询

DECLARE @Columns NVARCHAR(MAX)
DECLARE @MainQuery NVARCHAR(MAX)
SELECT  @Columns = ISNULL( @Columns + ',','') + QUOTENAME(CONVERT(VARCHAR(10),Data,20))  
FROM (SELECT DISTINCT Data FROM #Table) C
SET @MainQuery ='SELECT [Temperature],' + @Columns + '
FROM
(
  SELECT Data,Max_Temp,Min_Temp,Sr_Temp,Sr_Temp_work
  FROM #Table 
) as T
UNPIVOT
(
  Value FOR [Temperature] in (Max_Temp,Min_Temp,Sr_Temp,Sr_Temp_work)
) AS U
PIVOT
(
  MAX(Value) FOR [Data] IN ('+ @Columns +')
) as P
'

EXEC sp_executesql @MainQuery