我正在编写一个 SQL 查询来提取单个墨盒的打印使用情况。我的查询主体如下所示,但我无法选择一些特定数据来处理存储在单独表中的仪表读数。
以下查询列出了放入打印机的墨盒及其激活日期和停用日期。然后,我想使用 MeterReadings 表来查看使用基于 DeviceID 的 ActivatedDate 和 DeactivatedDate 在此期间的使用情况。到目前为止我所拥有的如下
SELECT Devices.DeviceID,
Devices.DeviceDescription,
DeviceConsumables.ConsumableVariantID,
ConsumableVariants.Type,
ConsumableDescriptions.Description,
MAX(ConsumableReadings.ReadingDate) as DeactivatedDate,
MIN(ConsumableReadings.ReadingDate) AS ActivatedDate,
ConsumableReadings.ChangedDate,
CASE ConsumableVariants.ColourID
WHEN 1 THEN MAX(MeterReadings.TotalMono) - MIN(MeterReadings.TotalMono)
ELSE MAX(MeterReadings.TotalColour) - MIN(MeterReadings.TotalColour)
END AS PrintingDiff,
ConsumableVariants.ExpectedPageCoverage,
ConsumableVariants.ExpectedPageYield
FROM Devices
LEFT JOIN DeviceConsumables ON Devices.DeviceID = DeviceConsumables.DeviceID
LEFT JOIN ConsumableVariants ON DeviceConsumables.ConsumableVariantID = ConsumableVariants.ConsumableVariantID
LEFT JOIN ConsumableReadings ON DeviceConsumables.ConsumableID = ConsumableReadings.ConsumableID
LEFT JOIN ConsumableDescriptions ON ConsumableVariants.DescriptionID = ConsumableDescriptions.ConsumableDescriptionID
LEFT JOIN MeterReadings ON DeviceConsumables.DeviceID = MeterReadings.DeviceID
WHERE ConsumableVariants.Type = '3' -- To only get toner cartridges
AND Devices.DeviceID = '24'
AND MeterReadings.ScanDateTime > MIN(ConsumableReadings.ReadingDate)
AND MeterReadings.ScanDateTime < MAX(ConsumableReadings.ReadingDate)
GROUP BY devices.DeviceID, Devices.DeviceDescription,
DeviceConsumables.ConsumableVariantID, ConsumableVariants.Type, ConsumableDescriptions.Description,
ConsumableReadings.ChangedDate, ConsumableVariants.ColourID, ConsumableVariants.ExpectedPageCoverage,
ConsumableVariants.ExpectedPageYield
ORDER BY Devices.DeviceID
This is currently generating the error "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."
The calculated fields ActivatedDate and DeactivatedDate are the date ranges I will require. I want to use the case statement to select MAX(MeterReadings.TotalMono) - MIN(MeterReadings.TotalMono) for black and white or MAX(MeterReadings.TotalColour) - MIN(MeterReadings.TotalColour) for colour. This would effectively give me the usage as readings can only go upwards. This would hopefully give me the starting point usage with the MIN and ending point usage with the MAX for the specific DeviceID.
As shown above I'm joining on my MeterReadings table on DeviceID.
I can't figure out how to get the MeterReadings for device x between y and z (where x is DeviceID, y is ActivatedDate and z is DeactivatedDate) so I can then add a calculated column into the case statement. Any help appreciated.
-- Edit For brevity I won't add all the schema in here but what should be enough.
Devices - list of all known devices
DeviceID
DeviceDescription
lots of extra fields that describe the device
DeviceConsumables - What devices use what consumables
ConsumableID
DeviceID - Forign key to device
ConsumableVariantID - Forign key to ConsumableVariant
ConsumableVariant - list of all the consumable variants there are
ConsumableVariantID
Type - 3 here indicates toner, what I'm interested in
ConsumableReadings
ReadingID - PK
ConsumableID - forign key to DeviceConsumables
ReadingDate - last time a reading was taken
ChangedDate - last time a new cartridge was inserted
MeterReadings
ReadingID - PK not to do with PK of consumablereadings
DeviceID
ScanDateTime - time usage scan was taken
TotalMono - total mono at scan time
TotalColour Total colour at scan time