0

我有 2 个表需要从中查询不同的数据。公共链接是日期时间列。我试图合并查询,但不断收到各种错误......

最后的查询应该给我:date, Peak Power each Day, KWHOpenHrs, KWHClosedTotal

任何帮助表示赞赏。

2表如下

Peak KVA桌子

Date | Power_Apparent_Total

询问:

SELECT
   CAST(t.HD as DATE), MAX(t.Power_Apparent_Total) as 'Peak Power Each Day'
FROM [Peak kVA] t
GROUP BY CAST(t.HD AS DATE)

Daily Consumption

Date | KWH Open | KWH ClosedandOccupied | KWH ClosedandnotOccupied

询问:

select date, 
max(value) kWhOpenHrs,
sum(case when col in ('zkWhClsedAndOccupied', 'zkWhClsedAndNotOccupied') then value   end) kWhClsedTotal
from 
(
  select date, 'zkWhOpenHrs' as col, kWhOpenHours as value
  from dbo.tblDailyPowerConsumption
  union all
  select date, 'zkWhClsedAndNotOccupied' as col, kWhClosedAndNotOccupied as value
  from dbo.tblDailyPowerConsumption
  union all
  select date, 'zkWhClsedAndOccupied' as col, kWhClosedAndOccupied as value
  from dbo.tblDailyPowerConsumption
) src
WHERE MONTH(date) = '12' 
group by date
4

2 回答 2

0

首先执行此请求。您的日期类型不仅包含日期,还包含时间

SELECT [Peak kVA].HD AS [date], [Peak kVA].Power_Apparent_Total,
       tblDailyPowerConsumption.kWhOpenHours, 
       tblDailyPowerConsumption.kWhClosedAndOccupied, 
       tblDailyPowerConsumption.kWhClosedAndNotOccupied 
FROM dbo.[Peak kVA] LEFT JOIN dbo.tblDailyPowerConsumption 
                      ON CAST([Peak kVA].HD AS date) = CAST(tblDailyPowerConsumption.Date AS date) 
                        AND MONTH(tblDailyPowerConsumption.Date) = '12'
于 2013-02-18T10:57:21.717 回答
0
select date, [Peak Power each Day], KWHOpenHrs, KWHClosedTotal
from
   (SELECT
        CAST(t.HD as DATE), MAX(t.Power_Apparent_Total) as 'Peak Power Each Day'
    FROM [Peak kVA] t
    GROUP BY CAST(t.HD AS DATE)) peak
inner join
   (select date, 
    max(value) kWhOpenHrs,
    sum(case when col in 
       ('zkWhClsedAndOccupied', 'zkWhClsedAndNotOccupied') 
       then value end) kWhClsedTotal
   from 
   (
   select date, 'zkWhOpenHrs' as col, kWhOpenHours as value
   from dbo.tblDailyPowerConsumption
   union all
   select date, 'zkWhClsedAndNotOccupied' as col, kWhClosedAndNotOccupied as value
   from dbo.tblDailyPowerConsumption
   union all
   select date, 'zkWhClsedAndOccupied' as col, kWhClosedAndOccupied as value
   from dbo.tblDailyPowerConsumption
   ) src
   WHERE MONTH(date) = '12' 
   group by date) daily
on peak.date = daily.date
于 2013-12-26T19:13:27.307 回答