我在下面的查询中获取了设备全天开启的百分比时间,但是由于数据中有重复项,我不能轻易摆脱,最好的方法是什么?< / p>
我尝试过使用distinct和Max(dtReading)
system ('taskkill /IM Winium.Desktop.Driver.exe /f')
查询输出
SELECT DISTINCT
workStationNo,
controllerID,
CAST(DATEPART(DAY, DATEADD(DAY, DATEDIFF(DAY, 0, MAX(dtReading)), 0)) AS varchar) AS date,
(CAST(COUNT(*) AS float(1)) / CAST((DATEPART(HOUR, GETDATE()) * 30) AS float(1)) * CAST(100 AS float(1))) AS dayPercentage
FROM Controller
INNER JOIN ManufacturingLayout
ON Controller.machID = ManufacturingLayout.workStationID
INNER JOIN ReaderData
ON Controller.ctrlID = ReaderData.controllerID
WHERE dtReading >= CONVERT(datetime, '09/05/2018', 103)
AND dtReading <= DATEADD(HOUR, 24, CONVERT(datetime, '16/05/2018 23:59:59', 103))
AND (EventType = '(0x05)Open switch')
GROUP BY controllerID,
DATEADD(DAY, DATEDIFF(DAY, 0, dtReading), 0),
workStationNo
ORDER BY 1, 2
源数据
Workstation no controllerID date dayPercentage
ZW01006 38 15 80.20833
ZW01006 38 16 142.0833
ZW01007 30 15 62.5
ZW01007 30 16 120.2083
ZW01008 31 15 92.70833
ZW01008 31 16 141.0417
ZW01010 40 15 0.625
答案 0 :(得分:2)
您可以为JOIN
到ReaderData
使用派生表,以便在JOIN
之前删除重复项:
...
INNER JOIN (SELECT DISTINCT controllerID, dtReading
FROM ReaderData) ReaderData
...