通过SQL 2008 R2将两个表添加到单独的列中
大家好,我正努力在一个查询中将两个表中的数据拉入单独的列中 目前我拥有的代码如下所示通过SQL 2008 R2将两个表添加到单独的列中,sql,sql-server,sql-server-2008-r2,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.[
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;
Month Day Hour hourly_kWh_10T hourly_kWh_4T shift
5 22 6 269.278551 834.679665 D
现在我得到了这个结果
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之间的月、日、小时是相同的
结果应该是这样的
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;
Month Day Hour hourly_kWh_10T hourly_kWh_4T shift
5 22 6 269.278551 834.679665 D
有几种不同的方法可以做到这一点:如前所述,
CASE
表达式可以很好地工作,但是,由于您实际上是从不同的表中提取数据,因此我将使用联合
执行以下操作,并添加一个外部SELECT
和groupby
语句,使用聚合清除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保留关键字(
Month
,Shift
,等等)。如果没有你的数据,我不能完全确定,但是这个查询应该会给出你希望的结果 将原始查询的结果作为起点(因为我既没有DDL也没有原始数据),您可以这样旋转它(此处的#t包含上述结果):
您可以使用
大小写表达式
或使用枢轴
来实现这一点。感谢您的反馈,请您向我解释为什么要添加额外的外部选择部分,特别是Max(*)?ThanksI将联合中的三列转换为六列,每列三列用于您正在使用的两组数据。这意味着任何给定的行在六个字段中的三个字段中总是有空值(10T在4T字段中返回空值,反之亦然)。为了将数据汇总到一行,MAX()函数将检索不为null的值,从而有效地将联合中的单独行与您要查找的值组合成一行。这只会起作用,因为您确信任何给定的输出行都有一个实际值。