t.monitorId,
t.temperature,
t.collectTime,
t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
LEFT JOIN ems_base_monitor_info ebmi ON t.monitorId = ebmi.monitor_code
AND (ebmi.monitor_type IN (5, 6) OR ebmi.monitor_type IS NULL)
AND (t.temperature IS NULL OR (t.temperature BETWEEN 0 AND 79))
AND (
(ebmi.monitor_type = 5 AND t.temperature > 0) OR
(ebmi.monitor_type = 6 AND t.temperature > 0) OR
(ebmi.monitor_type NOT IN (5, 6) OR ebmi.monitor_type IS NULL)
)
AND t.collectTime >= #{startTime}
AND t.collectTime < #{endTime}
SELECT COUNT(DISTINCT sub.monitorId)
FROM (
SELECT t.monitorId
FROM ${tableName} t
) sub
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT ROUND(AVG(temperature), 2) AS avgLatestTemp
FROM latest WHERE rn = 1
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
),
latest_only AS (
SELECT monitorId, temperature, monitorName
FROM latest WHERE rn = 1
),
max_val AS (
SELECT TOP 1 monitorId AS maxTempMonitorId, temperature AS maxLatestTemp
FROM latest_only ORDER BY temperature DESC
),
min_val AS (
SELECT TOP 1 monitorId AS minTempMonitorId, temperature AS minLatestTemp
FROM latest_only ORDER BY temperature ASC
)
SELECT m.maxTempMonitorId, m.maxLatestTemp,
n.minTempMonitorId, n.minLatestTemp
FROM max_val m CROSS JOIN min_val n
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT TOP (#{topN}) monitorId, monitorName, temperature, collectTime
FROM latest WHERE rn = 1
ORDER BY temperature DESC
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT TOP (#{topN}) monitorId, monitorName, temperature, collectTime
FROM latest WHERE rn = 1
ORDER BY temperature ASC
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT monitorId, monitorName, temperature, collectTime,
DATEDIFF(SECOND, collectTime, GETUTCDATE()) AS ageSeconds
FROM latest WHERE rn = 1
ORDER BY ageSeconds DESC
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, recodeTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT monitorId, monitorName, temperature, collectTime, recodeTime,
DATEDIFF(SECOND, collectTime, recodeTime) AS delaySeconds,
DATEDIFF(SECOND, collectTime, GETUTCDATE()) AS staleSeconds
FROM latest WHERE rn = 1
ORDER BY collectTime DESC, monitorId
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, recodeTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT monitorId, monitorName, temperature, collectTime, recodeTime,
DATEDIFF(SECOND, collectTime, recodeTime) AS delaySeconds
FROM latest WHERE rn = 1 AND temperature >= #{highTempThreshold}
ORDER BY temperature DESC
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, recodeTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT monitorId, monitorName, temperature, collectTime, recodeTime,
DATEDIFF(SECOND, collectTime, recodeTime) AS delaySeconds
FROM latest WHERE rn = 1 AND temperature <= #{lowTempThreshold}
ORDER BY temperature ASC
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, recodeTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT monitorId, monitorName, temperature, collectTime,
DATEDIFF(SECOND, collectTime, GETUTCDATE()) AS staleSeconds
FROM latest WHERE rn = 1
AND DATEDIFF(SECOND, collectTime, GETUTCDATE()) >= #{staleThreshold}
ORDER BY staleSeconds DESC
WITH all_data AS (
SELECT t.monitorId, t.temperature, t.collectTime, t.recodeTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
latest AS (
SELECT monitorId, temperature, collectTime, recodeTime, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime DESC, recodeTime DESC) AS rn
FROM all_data
)
SELECT monitorId, monitorName, collectTime, recodeTime,
DATEDIFF(SECOND, collectTime, recodeTime) AS delaySeconds
FROM latest WHERE rn = 1
ORDER BY delaySeconds DESC
SELECT
FORMAT(t.collectTime, 'yyyy-MM-dd HH:mm:00') AS statTime,
t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp,
COUNT(*) AS sampleCount
FROM ${tableName} t
t.monitorId = #{monitorId}
GROUP BY FORMAT(t.collectTime, 'yyyy-MM-dd HH:mm:00'), t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY statTime
SELECT
FORMAT(t.collectTime, 'yyyy-MM-dd HH:00:00') AS statTime,
t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp,
ROUND(MAX(t.temperature), 2) AS maxTemp,
ROUND(MIN(t.temperature), 2) AS minTemp,
COUNT(*) AS sampleCount
FROM ${tableName} t
t.monitorId = #{monitorId}
GROUP BY FORMAT(t.collectTime, 'yyyy-MM-dd HH:00:00'), t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY statTime
SELECT
FORMAT(t.collectTime, 'yyyy-MM-dd HH:mm:00') AS statTime,
t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp
FROM ${tableName} t
AND t.monitorId IN
#{mid}
GROUP BY FORMAT(t.collectTime, 'yyyy-MM-dd HH:mm:00'), t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY statTime, monitorId
SELECT
FORMAT(CAST(t.collectTime AS DATE), 'yyyy-MM-dd') AS statTime,
NULL AS monitorId,
NULL AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp,
ROUND(MAX(t.temperature), 2) AS maxTemp,
ROUND(MIN(t.temperature), 2) AS minTemp,
COUNT(*) AS sampleCount
FROM ${tableName} t
GROUP BY CAST(t.collectTime AS DATE)
ORDER BY statTime
WITH base AS (
SELECT t.monitorId, t.temperature, t.collectTime
FROM ${tableName} t
AND t.monitorId = #{monitorId}
),
seq AS (
SELECT monitorId, collectTime, temperature,
LAG(collectTime) OVER (PARTITION BY monitorId ORDER BY collectTime) AS prevTime,
LAG(temperature) OVER (PARTITION BY monitorId ORDER BY collectTime) AS prevTemp
FROM base
)
SELECT monitorId,
collectTime AS statTime,
prevTime,
prevTemp,
temperature,
ROUND(
CAST(temperature - prevTemp AS FLOAT) /
NULLIF(CAST(DATEDIFF(SECOND, prevTime, collectTime) AS FLOAT), 0) * 60,
4
) AS changeRate
FROM seq WHERE prevTime IS NOT NULL
ORDER BY collectTime
WITH base AS (
SELECT t.monitorId, t.temperature, t.collectTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
ranked AS (
SELECT monitorId, collectTime, temperature, monitorName,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY temperature DESC, collectTime ASC) AS rnMax,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY temperature ASC, collectTime ASC) AS rnMin
FROM base
)
SELECT monitorId, monitorName,
MAX(CASE WHEN rnMax = 1 THEN temperature END) AS peakTemp,
MAX(CASE WHEN rnMax = 1 THEN collectTime END) AS peakTime,
MAX(CASE WHEN rnMin = 1 THEN temperature END) AS valleyTemp,
MAX(CASE WHEN rnMin = 1 THEN collectTime END) AS valleyTime
FROM ranked
GROUP BY monitorId, monitorName
SELECT sub.tempBucket, COUNT(*) AS sampleCount
FROM (
SELECT t.temperature,
CASE
WHEN t.temperature < 15 THEN '<15'
WHEN t.temperature < 20 THEN '15-20'
WHEN t.temperature < 25 THEN '20-25'
WHEN t.temperature < 30 THEN '25-30'
ELSE '>=30'
END AS tempBucket
FROM ${tableName} t
) sub
GROUP BY sub.tempBucket
ORDER BY sub.tempBucket
SELECT FLOOR(t.temperature) AS tempBin, COUNT(*) AS sampleCount
FROM ${tableName} t
GROUP BY FLOOR(t.temperature)
ORDER BY tempBin
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
t.temperature
FROM ${tableName} t
ORDER BY monitorId, temperature
SELECT
FORMAT(CAST(t.collectTime AS DATE), 'yyyy-MM-dd') AS statDate,
ROUND(AVG(t.temperature), 2) AS avgTemp
FROM ${tableName} t
GROUP BY CAST(t.collectTime AS DATE)
ORDER BY statDate
SELECT
FORMAT(CAST(t.collectTime AS DATE), 'yyyy-MM-dd') AS statDate,
DATEPART(HOUR, t.collectTime) AS statHour,
ROUND(AVG(t.temperature), 2) AS avgTemp
FROM ${tableName} t
GROUP BY CAST(t.collectTime AS DATE), DATEPART(HOUR, t.collectTime)
ORDER BY statDate, statHour
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
t.temperature,
t.collectTime,
'HIGH_TEMP' AS anomalyType
FROM ${tableName} t
t.temperature >= #{highTempThreshold}
ORDER BY collectTime DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
t.temperature,
t.collectTime,
'LOW_TEMP' AS anomalyType
FROM ${tableName} t
t.temperature <= #{lowTempThreshold}
ORDER BY collectTime DESC
WITH base AS (
SELECT t.monitorId, t.temperature, t.collectTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
flagged AS (
SELECT monitorId, temperature, collectTime, monitorName,
CASE WHEN temperature >= #{highTempThreshold} THEN 1 ELSE 0 END AS isHigh
FROM base
),
grouped AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY monitorId ORDER BY collectTime) -
ROW_NUMBER() OVER (PARTITION BY monitorId, isHigh ORDER BY collectTime) AS grp
FROM flagged
)
SELECT monitorId, monitorName,
MIN(collectTime) AS startTime,
MAX(collectTime) AS endTime,
MAX(temperature) AS maxTemp,
COUNT(*) AS sampleCount
FROM grouped WHERE isHigh = 1
GROUP BY monitorId, monitorName, grp
HAVING COUNT(*) >= 2
ORDER BY monitorId, startTime
WITH base AS (
SELECT t.monitorId, t.temperature, t.collectTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
seq AS (
SELECT monitorId, collectTime, temperature, monitorName,
LAG(collectTime) OVER (PARTITION BY monitorId ORDER BY collectTime) AS prevTime,
LAG(temperature) OVER (PARTITION BY monitorId ORDER BY collectTime) AS prevTemp
FROM base
)
SELECT monitorId, monitorName, collectTime, prevTime,
prevTemp AS prevTemp,
temperature,
ROUND(
CAST(temperature - prevTemp AS FLOAT) /
NULLIF(CAST(DATEDIFF(SECOND, prevTime, collectTime) AS FLOAT), 0) * 60,
4
) AS risePerMin
FROM seq WHERE prevTime IS NOT NULL
AND CAST(temperature - prevTemp AS FLOAT) /
NULLIF(CAST(DATEDIFF(SECOND, prevTime, collectTime) AS FLOAT), 0) * 60 >= #{rapidRiseThreshold}
ORDER BY risePerMin DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
FORMAT(t.collectTime, 'yyyy-MM-dd HH:00:00') AS statTime,
ROUND(STDEVP(t.temperature), 4) AS tempStddev,
'JITTER' AS anomalyType
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId),
FORMAT(t.collectTime, 'yyyy-MM-dd HH:00:00')
HAVING STDEVP(t.temperature) >= #{stddevThreshold}
ORDER BY tempStddev DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY avgTemp DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(STDEVP(t.temperature), 4) AS tempStddev
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY tempStddev ASC
WITH today_avg AS (
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
AVG(t.temperature) AS avgTemp
FROM ${tableName} t
t.collectTime >= #{todayStartTime} AND t.collectTime < #{todayEndTime}
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
),
yesterday_avg AS (
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
AVG(t.temperature) AS avgTemp
FROM ${tableName} t
t.collectTime >= #{yesterdayStartTime} AND t.collectTime < #{yesterdayEndTime}
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
)
SELECT t.monitorId, t.monitorName,
ROUND(t.avgTemp, 2) AS todayAvg,
ROUND(y.avgTemp, 2) AS yesterdayAvg,
ROUND(t.avgTemp - y.avgTemp, 2) AS diffValue
FROM today_avg t
LEFT JOIN yesterday_avg y ON t.monitorId = y.monitorId
ORDER BY diffValue DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
MAX(t.temperature) AS maxTemp
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY maxTemp DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
MAX(t.temperature) - MIN(t.temperature) AS tempRange
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY tempRange DESC
SELECT sub.delayBucket, COUNT(*) AS sampleCount
FROM (
SELECT t.monitorId, t.collectTime, t.recodeTime,
CASE
WHEN DATEDIFF(SECOND, t.collectTime, t.recodeTime) < 10 THEN '<10s'
WHEN DATEDIFF(SECOND, t.collectTime, t.recodeTime) < 30 THEN '10-30s'
WHEN DATEDIFF(SECOND, t.collectTime, t.recodeTime) < 60 THEN '30-60s'
ELSE '>=60s'
END AS delayBucket
FROM ${tableName} t
) sub
GROUP BY sub.delayBucket
ORDER BY sub.delayBucket
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
t.temperature,
t.collectTime,
t.recodeTime,
DATEDIFF(SECOND, t.collectTime, t.recodeTime) AS delaySeconds
FROM ${tableName} t
t.recodeTime < t.collectTime
ORDER BY collectTime DESC
WITH base AS (
SELECT t.monitorId, t.collectTime,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName
FROM ${tableName} t
),
seq AS (
SELECT monitorId, collectTime, monitorName,
LAG(collectTime) OVER (PARTITION BY monitorId ORDER BY collectTime) AS prevTime
FROM base
)
SELECT monitorId, monitorName, prevTime, collectTime,
DATEDIFF(SECOND, prevTime, collectTime) AS gapSeconds
FROM seq WHERE prevTime IS NOT NULL
AND DATEDIFF(SECOND, prevTime, collectTime) >= #{gapThreshold}
ORDER BY gapSeconds DESC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
COUNT(*) AS actualCount,
#{expectedCount} AS expectedCount,
ROUND(CAST(COUNT(*) AS FLOAT) / #{expectedCount}, 4) AS completenessRate
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY completenessRate ASC
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
COUNT(*) AS actualCount,
MIN(t.collectTime) AS firstTime,
MAX(t.collectTime) AS lastTime
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY actualCount DESC
WITH stage AS (
SELECT DISTINCT monitorId, timeBucket, tempBucket FROM (
SELECT DISTINCT t.monitorId,
CASE
WHEN DATEPART(HOUR, t.collectTime) < 6 THEN 'T1'
WHEN DATEPART(HOUR, t.collectTime) < 12 THEN 'T2'
WHEN DATEPART(HOUR, t.collectTime) < 18 THEN 'T3'
ELSE 'T4'
END AS timeBucket,
CASE
WHEN t.temperature < 15 THEN '<15'
WHEN t.temperature < 20 THEN '15-20'
WHEN t.temperature < 25 THEN '20-25'
ELSE '>=25'
END AS tempBucket
FROM ${tableName} t
) raw_stage
),
flow AS (
SELECT
a.timeBucket + '_' + a.tempBucket AS fromNode,
b.timeBucket + '_' + b.tempBucket AS toNode,
COUNT(*) AS flowCount
FROM stage a
INNER JOIN stage b ON a.monitorId = b.monitorId
AND (
(a.timeBucket = 'T1' AND b.timeBucket = 'T2') OR
(a.timeBucket = 'T2' AND b.timeBucket = 'T3') OR
(a.timeBucket = 'T3' AND b.timeBucket = 'T4')
)
GROUP BY a.timeBucket, a.tempBucket, b.timeBucket, b.tempBucket
)
SELECT fromNode, toNode, flowCount FROM flow
ORDER BY fromNode, toNode
SELECT
CONVERT(VARCHAR(19), statBucket, 120) AS statTime,
monitorId, monitorName, avgTemp
FROM (
SELECT
DATEADD(HOUR, DATEDIFF(HOUR, 0, t.collectTime), 0)
DATEADD(MINUTE, (DATEDIFF(MINUTE, 0, t.collectTime) / 15) * 15, 0)
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, t.collectTime), 0)
AS statBucket,
t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp
FROM ${tableName} t
GROUP BY
DATEADD(HOUR, DATEDIFF(HOUR, 0, t.collectTime), 0)
DATEADD(MINUTE, (DATEDIFF(MINUTE, 0, t.collectTime) / 15) * 15, 0)
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, t.collectTime), 0)
,
t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
) sub
ORDER BY statTime, monitorId
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp,
COUNT(*) AS sampleCount
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY avgTemp DESC
SELECT
CASE
WHEN t.temperature < 15 THEN '<15'
WHEN t.temperature < 20 THEN '15-20'
WHEN t.temperature < 25 THEN '20-25'
WHEN t.temperature < 30 THEN '25-30'
ELSE '>=30'
END AS tempBucket,
t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
COUNT(*) AS sampleCount
FROM ${tableName} t
GROUP BY
CASE
WHEN t.temperature < 15 THEN '<15'
WHEN t.temperature < 20 THEN '15-20'
WHEN t.temperature < 25 THEN '20-25'
WHEN t.temperature < 30 THEN '25-30'
ELSE '>=30'
END,
t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY tempBucket, monitorId
SELECT t.monitorId,
COALESCE(ebmi.monitor_name, t.monitorId) AS monitorName,
ROUND(AVG(t.temperature), 2) AS avgTemp,
ROUND(MAX(t.temperature), 2) AS maxTemp,
ROUND(MIN(t.temperature), 2) AS minTemp,
ROUND(STDEVP(t.temperature), 4) AS tempStddev,
ROUND(AVG(CAST(DATEDIFF(SECOND, t.collectTime, t.recodeTime) AS FLOAT)), 2) AS avgDelay
FROM ${tableName} t
GROUP BY t.monitorId, COALESCE(ebmi.monitor_name, t.monitorId)
ORDER BY avgTemp DESC