通过SQL 2008 R2将两个表添加到单独的列中

时间:2014-06-04 12:07:30

标签: sql sql-server sql-server-2008-r2

大家好,我正在努力将两个表中的数据分成一个查询中的单独列

目前我的代码看起来像这样

Declare @Begin Varchar(60),
        @End Varchar(60)
Set @Begin = '2014-05-22  06:00:00' 
Set @End = '2014-06-01  06:00:00'

SELECT 
    ID='10T'
    ,DATEPART(month,A.[DateTime]) Month
    ,DATEPART(day,A.[DateTime]) Day
    ,DATEPART(hour,A.[DateTime]) Hour
    ,avg(A.[kw]) hourly_kWh
    ,avg(A.[KVAr]) hourly_kVarh
    ,avg(A.[KVAReal]) hourly_kVAh
    ,(case when(DATEPART(hour,A.[DateTime]) =6 and DATEPART(hour,A.[DateTime]) 18) then 'D' else 'N' end)  shift
  FROM [POWER].[dbo].[IT10t_PAC3200] AS A
  where DateTime = @Begin and DateTime  @End
  group by DATEPART(Hour,[DateTime]),DATEPART(Day,[DateTime]),DATEPART(Month,[DateTime])

union

SELECT 
    ID='4T'
    ,DATEPART(month,B.[DateTime]) Month
    ,DATEPART(day,B.[DateTime]) Day
    ,DATEPART(hour,B.[DateTime]) as Hour
    ,avg(B.[kw]) hourly_kWh
    ,avg(B.[KVAr]) hourly_kVarh
    ,avg(B.[KVAReal]) hourly_kVAh
    ,(case when(DATEPART(hour,B.[DateTime]) =6 and DATEPART(hour,B.[DateTime]) 18) then 'D' else 'N' end)  shift
  FROM [POWER].[dbo].[ITfurnace] AS B 
  where DateTime = @Begin and DateTime  @End
  group by DATEPART(Hour,[DateTime]),DATEPART(Day,[DateTime]),DATEPART(Month,[DateTime])

  order by ID,month,day,hour asc;

现在这给了我这个结果

ID  Month   Day Hour    hourly_kWh  hourly_kVarh    hourly_kVAh shift
10T 5       22  6       269.278551  80.771587      294.038997       D
10T 5       22  7       241.213296  75.991689      268.085872       D
10T 5       22  8       283.925     93.302777      319.211111       D
4T  5       22  6       834.679665  238.245125     870.640668       D
4T  5       22  7       548.476454  164.764542     576.814404       D
4T  5       22  8       834.111111  237.055555     870.888888       D

我尝试使用连接功能但没有成功(我不理解它背后的逻辑)你可以帮助我编辑我的查询,将现在在不同行中的ID分开移动到列。因为月,日,小时在不同的Id之间是相同的

结果看起来应该是这样的

Month   Day Hour    hourly_kWh_10T  hourly_kWh_4T   shift
5       22  6       269.278551      834.679665      D

2 个答案:

答案 0 :(得分:0)

你可以采用不同的方式做到这一点:如上所述,CASE表达式可以正常运行,但由于你实际上从不同的表中提取数据,我会使用你的UNION执行以下操作并使用聚合添加外部SELECTGROUP BY语句以清除NULL值。

Declare @Begin Varchar(60),
        @End Varchar(60)
Set @Begin = '2014-05-22  06:00:00' 
Set @End = '2014-06-01  06:00:00'


SELECT 
    [Month], 
    [Day], 
    [Hour], 
    MAX(hourly_kWh_10T), 
    MAX(hourly_kVarh_10T), 
    MAX(hourly_kVAh_10T),
    MAX(hourly_kWh_4T), 
    MAX(hourly_kVarh_4T), 
    MAX(hourly_kVAh_4T)
    [Shift]
FROM 
  (
    SELECT 
        ID='10T'
        ,DATEPART(month,A.[DateTime]) Month
        ,DATEPART(day,A.[DateTime]) Day
        ,DATEPART(hour,A.[DateTime]) Hour
        ,avg(A.[kw]) hourly_kWh_10T
        ,avg(A.[KVAr]) hourly_kVarh_10T
        ,avg(A.[KVAReal]) hourly_kVAh_10T
        ,NULL hourly_kWh_4T
        ,NULL hourly_kVarh_4T
        ,NULL hourly_kVah_4T
        ,(case when(DATEPART(hour,A.[DateTime]) =6 and DATEPART(hour,A.[DateTime]) 18) then 'D' else 'N' end)  shift
      FROM [POWER].[dbo].[IT10t_PAC3200] AS A
      where DateTime = @Begin and DateTime  @End
      group by DATEPART(Hour,[DateTime]),DATEPART(Day,[DateTime]),DATEPART(Month,[DateTime])

    union

    SELECT 
        ID='4T'
        ,DATEPART(month,B.[DateTime]) Month
        ,DATEPART(day,B.[DateTime]) Day
        ,DATEPART(hour,B.[DateTime]) as Hour
        ,NULL
        ,NULL
        ,NULL
        ,avg(B.[kw]) hourly_kWh_4T
        ,avg(B.[KVAr]) hourly_kVarh_4T
        ,avg(B.[KVAReal]) hourly_kVAh_4T
        ,(case when(DATEPART(hour,B.[DateTime]) =6 and DATEPART(hour,B.[DateTime]) 18) then 'D' else 'N' end)  shift
      FROM [POWER].[dbo].[ITfurnace] AS B 
      where DateTime = @Begin and DateTime  @End
      group by DATEPART(Hour,[DateTime]),DATEPART(Day,[DateTime]),DATEPART(Month,[DateTime])
  ) s
GROUP BY 
    [Month],
    [Day],
    [Hour],
    [Shift]

我肯定会避免使用您在[方括号]中看到的SQL Server保留关键字(MonthShift等)。没有你的数据运行,我不能完全确定,但这个查询应该给你你希望的结果。

答案 1 :(得分:0)

以原始查询的结果作为起点(因为我既没有DDL也没有原始数据),你可以像这样窥探它(这里的#t包含上述结果):

select Month, Day, Hour, shift, [10T] as hourly_kWh_10T, [4T] as hourly_kWh_4T 
from (
    select ID, Month, Day, Hour, shift, hourly_kWh from #t
) as source
pivot (
    max(hourly_kWh)
    for id in ([10T], [4T])
) as pvt