0

異なるデータを照会する必要がある 2 つのテーブルがあります。共通のリンクは datetime 列です。クエリをマージしようとしましたが、さまざまなエラーが発生し続けます...

最後のクエリは次のようになります。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 に答える