add to the query to create columns that reflects the timestamp when the event value…
- User
add to the query to create columns that reflects the timestamp when the event value changes from 0 to 1 as start_time and when it changes from 1 to 0 as end_time:
SELECT
(from_unixtime(event_timestamp/1000))AS DATE,
CASE
WHEN event_value = 0 THEN 'OFF'
WHEN event_value = 1 THEN 'ON'
END AS Furnace_Status
FROM
database.table
WHERE
field_name = 'bV1_Status' AND p_id = '0000'
GROUP BY
DATE, event_value
ORDER BY DATE DESC
LIMIT 100
;
- Assistant
SELECT
(from_unixtime(event_timestamp/1000))AS DATE,
CASE
WHEN event_value = 0 THEN 'OFF'
WHEN event_value = 1 THEN 'ON'
END AS Furnace_Status,
CASE
WHEN event_value = 0 AND LAG(event_value) OVER (ORDER BY event_timestamp ASC) = 1 THEN from_unixtime(event_timestamp/1000)
WHEN event_value = 1 AND LAG(event_value) OVER (ORDER BY event_timestamp ASC) = 0 THEN from_unixtime(event_timestamp/1000)
END AS start_time,
CASE
WHEN event_value = 1 AND LEAD(event_value) OVER (ORDER BY event_timestamp ASC) = 0 THEN from_unixtime(event_timestamp/1000)
WHEN event_value = 0 AND LEAD(event_value) OVER (ORDER BY event_timestamp ASC) = 1 THEN from_unixtime(event_timestamp/1000)
END AS end_time
FROM
database.table
WHERE
field_name = 'bV1_Status' AND p_id = '0000'
GROUP BY
DATE, event_value
ORDER BY DATE DESC
LIMIT 100;
- User
fix it, the start_time and end_time columns are giving me null values
- Assistant
To fix the issue where the start_time and end_time columns are returning null values, you need to modify the query by using the LAG and LEAD functions with an appropriate window frame (PARTITION BY clause):
SELECT (from_unixtime(event_timestamp/1000)) AS DATE, CASE WHEN event_value = 0 THEN 'OFF' WHEN event_value = 1 THEN 'ON' END AS Furnace_Status, CASE WHEN event_value = 0 AND LAG(event_value) OVER (PARTITION BY event_value ORDER BY event_timestamp) = 1 THEN (from_unixtime(event_timestamp/1000)) WHEN event_value = 1 AND LAG(event_value) OVER (PARTITION BY event_value ORDER BY event_timestamp) = 0 THEN (from_unixtime(event_timestamp/1000)) END AS start_time, CASE WHEN event_value = 1 AND LEAD(event_value) OVER (PARTITION BY event_value ORDER BY event_timestamp) = 0 THEN (from_unixtime(event_timestamp/1000)) WHEN event_value = 0 AND LEAD(event_value) OVER (PARTITION BY event_value ORDER BY event_timestamp) = 1 THEN (from_unixtime(event_timestamp/1000)) END AS end_time FROM database.table WHERE field_name = 'bV1_Status' AND p_id = '0000' ORDER BY event_timestamp DESC LIMIT 100; - User
fix it, it is still showing null value
- Assistant
Apologies for the confusion. I've noticed that the original query is using GROUP BY, which means it aggregates the rows and eliminates the individual changes from 0 to 1 and from 1 to 0. To correctly capture those changes as start_time and end_time, we need to remove the GROUP BY clause and modify the query as follows:
SELECT (from_unixtime(event_timestamp/1000)) AS DATE, CASE WHEN event_value = 0 THEN 'OFF' WHEN event_value = 1 THEN 'ON' END AS Furnace_Status, CASE WHEN event_value = 0 AND LAG(event_value) OVER (ORDER BY event_timestamp) = 1 THEN (from_unixtime(event_timestamp/1000)) WHEN event_value = 1 AND LAG(event_value) OVER (ORDER BY event_timestamp) = 0 THEN (from_unixtime(event_timestamp/1000)) END AS start_time, CASE WHEN event_value = 1 AND LEAD(event_value) OVER (ORDER BY event_timestamp) = 0 THEN (from_unixtime(event_timestamp/1000)) WHEN event_value = 0 AND LEAD(event_value) OVER (ORDER BY event_timestamp) = 1 THEN (from_unixtime(event_timestamp/1000)) END AS end_time FROM database.table WHERE field_name = 'bV1_Status' AND p_id = '0000' ORDER BY event_timestamp DESC LIMIT 100; - User
it is still showing null values
- Assistant
Apologies for the confusion. To correctly handle the changes from 0 to 1 and 1 to 0 and avoid null values, we can use a subquery to calculate the start_time and end_time. Here's the modified query to achieve that:
SELECT date, Furnace_Status, CASE WHEN event_value = 0 AND LAG(event_value) OVER (ORDER BY event_timestamp) = 1 THEN start_time WHEN event_value = 1 AND LAG(event_value) OVER (ORDER BY event_timestamp) = 0 THEN from_unixtime(event_timestamp/1000) WHEN ROW_NUMBER() OVER (ORDER BY event_timestamp) = 1 THEN from_unixtime(event_timestamp/1000) ELSE NULL END AS start_time, CASE WHEN event_value = 1 AND LEAD(event_value) OVER (ORDER BY event_timestamp) = 0 THEN end_time WHEN event_value = 0 AND LEAD(event_value) OVER (ORDER BY event_timestamp) = 1 THEN from_unixtime(event_timestamp/1000) WHEN ROW_NUMBER() OVER (ORDER BY event_timestamp DESC) = 1 THEN from_unixtime(event_timestamp/1000) ELSE NULL END AS end_time FROM ( SELECT (from_unixtime(event_timestamp/1000)) AS date, CASE WHEN event_value = 0 THEN 'OFF' WHEN event_value = 1 THEN 'ON' END AS Furnace_Status, event_value, event_timestamp, LAG(event_timestamp) OVER (ORDER BY event_timestamp) AS start_time, LEAD(event_timestamp) OVER (ORDER BY event_timestamp) AS end_time FROM database.table WHERE field_name = 'bV1_Status' AND p_id = '0000' ORDER BY event_timestamp DESC LIMIT 100 ) AS subquery; - User
add to this query to display the device name
- Assistant
To display the device name in the query, you need to include the device name column in the subquery and adjust the column selections in the main query accordingly. Here's the modified query:
SELECT date, device_name, Furnace_Status, CASE WHEN event_value = 0 AND LAG(event_value) OVER (ORDER BY event_timestamp) = 1 THEN start_time WHEN event_value = 1 AND LAG(event_value) OVER (ORDER BY event_timestamp) = 0 THEN from_unixtime(event_timestamp/1000) WHEN ROW_NUMBER() OVER (ORDER BY event_timestamp) = 1 THEN from_unixtime(event_timestamp/1000) ELSE NULL END AS start_time, CASE WHEN event_value = 1 AND LEAD(event_value) OVER (ORDER BY event_timestamp) = 0 THEN end_time WHEN event_value = 0 AND LEAD(event_value) OVER (ORDER BY event_timestamp) = 1 THEN from_unixtime(event_timestamp/1000) WHEN ROW_NUMBER() OVER (ORDER BY event_timestamp DESC) = 1 THEN from_unixtime(event_timestamp/1000) ELSE NULL END AS end_time FROM ( SELECT (from_unixtime(event_timestamp/1000)) AS date, device_name, CASE WHEN event_value = 0 THEN 'OFF' WHEN event_value = 1 THEN 'ON' END AS Furnace_Status, event_value, event_timestamp, LAG(event_timestamp) OVER (ORDER BY event_timestamp) AS start_time, LEAD(event_timestamp) OVER (ORDER BY event_timestamp) AS end_time FROM database.table WHERE field_name = 'bV1_Status' AND p_id = '0000' ORDER BY event_timestamp DESC LIMIT 100 ) AS subquery; - User
fix the query so it only shows distinct values from furnace_status
- Assistant
To only show distinct values for
furnace_status, you can modify the query by using a subquery with the DISTINCT keyword. Here's the modified query:SELECT main.date, main.device_name, main.Furnace_Status, CASE WHEN main.event_value = 0 AND LAG(main.event_value) OVER (ORDER BY main.event_timestamp) = 1 THEN main.start_time WHEN main.event_value = 1 AND LAG(main.event_value) OVER (ORDER BY main.event_timestamp) = 0 THEN from_unixtime(main.event_timestamp/1000) WHEN ROW_NUMBER() OVER (ORDER BY main.event_timestamp) = 1 THEN from_unixtime(main.event_timestamp/1000) ELSE NULL END AS start_time, CASE WHEN main.event_value = 1 AND LEAD(main.event_value) OVER (ORDER BY main.event_timestamp) = 0 THEN main.end_time WHEN main.event_value = 0 AND LEAD(main.event_value) OVER (ORDER BY main.event_timestamp) = 1 THEN from_unixtime(main.event_timestamp/1000) WHEN ROW_NUMBER() OVER (ORDER BY main.event_timestamp DESC) = 1 THEN from_unixtime(main.event_timestamp/1000) ELSE NULL END AS end_time FROM ( SELECT DISTINCT (from_unixtime(event_timestamp/1000)) AS date, device_name, CASE WHEN event_value = 0 THEN 'OFF' WHEN event_value = 1 THEN 'ON' END AS Furnace_Status, event_value, event_timestamp, LAG(event_timestamp) OVER (ORDER BY event_timestamp) AS start_time, LEAD(event_timestamp) OVER (ORDER BY event_timestamp) AS end_time FROM database.table WHERE field_name = 'bV1_Status' AND p_id = '0000' ORDER BY event_timestamp DESC LIMIT 100 ) AS main;