blast_seconds
正しい値として表示される問題が発生しています。私の select サブクエリblast_seconds
では、メインの Select ステートメントと同じ時間内に を表示する必要があります。クエリが実行されるとYear
、Month
、ScheduledSeconds
、が一致しますが、列はすべての月を通して同じ値を示します。TotalDays
Totaltrucks
blast_seconds
これが私のクエリです:
SELECT DATEPART(YEAR, [Time]) AS [Year]
, DATEPART(MONTH, [Time]) AS [Month]
, SUM(Total) AS ScheduledSeconds
, COUNT(DISTINCT([Time])) AS TotalDays
, COUNT(DISTINCT(Equipment)) AS TotalTrucks
, (
SELECT SUM(CASE
WHEN dbo.reasons.status_id = '298'
THEN (dbo.by_operator_reasons.seconds)
ELSE 0
END)
FROM dbo.reasons
INNER JOIN by_operator_reasons
ON dbo.reasons.id = by_operator_reasons.reason_id
INNER JOIN equipment
ON by_operator_reasons.equipment_id = equipment.id
WHERE reasons.descrip LIKE 'Blast'
AND [Time] BETWEEN '2011-01-01' AND '2012-05-15'
AND by_operator_reasons.deleted_at IS NULL
AND equipment.type = 'Truck'
) AS Blast_Seconds
FROM by_equipment_times
WHERE [Time] BETWEEN '2011-01-01' and '2012-05-15'
AND equipment_type = 'Truck'
GROUP BY DATEPART(YEAR, [Time])
, DATEPART(MONTH, [Time])
ORDER BY DATEPART(YEAR, [Time]) ASC
, DATEPART(MONTH, [Time]) ASC
ここに私の現在の出力があります:
Year Month SchedSec Days TotalTrucks Blast_Seconds
---- ----- -------- ---- ----------- -------------
2011 1 51340448 31 20 4931156
2011 2 51979509 28 22 4931156
2011 3 58845600 31 22 4931156
2011 4 59121967 30 24 4931156
2011 5 66857271 31 25 4931156
2011 6 67306766 30 28 4931156
2011 7 76976358 31 30 4931156
2011 8 80393145 31 30 4931156
2011 9 75556005 30 30 4931156
2011 10 77741205 31 29 4931156
2011 11 75272400 30 29 4931156
2011 12 77691044 31 29 4931156
2012 1 77683752 31 29 4931156
2012 2 72662400 29 29 4931156
2012 3 77574538 31 29 4931156
2012 4 75172177 30 29 4931156
2012 5 37584000 15 29 4931156