我有 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