-- View: v_sensordata_current

-- DROP VIEW v_sensordata_current;

CREATE OR REPLACE VIEW v_sensordata_current AS 
 SELECT r1.rn,
    r1.id,
    r1.sensor_type,
    r1.data_read,
    to_timestamp(r1.time_read) AT TIME ZONE 'UTC' as time_read
   FROM ( SELECT row_number() OVER (PARTITION BY sensordata.sensor_type ORDER BY sensordata.time_read DESC) AS rn,
            sensordata.id,
            sensordata.sensor_type,
            sensordata.data_read,
            sensordata.time_read,
            sensordata.time_changed
           FROM sensordata) r1
  WHERE r1.rn = 1;
