我想从“主题”行转换此输出...
SMARTBASE/N0184/1/MOISTURE/value
SMARTBASE/N0184/1/MOISTURE/unit
SMARTBASE/N0184/1/MOISTURE/timestamp
SMARTBASE/N0184/1/CONDUCTIVITY/value
SMARTBASE/N0184/1/CONDUCTIVITY/unit
SMARTBASE/N0184/1/CONDUCTIVITY/timestamp
到新表,如:
SENSORS|MOISTURE(value)|MOISTURE(unit)|CONDUCTIVITY(value)|CONDUCTIVITY(unit)
N0184|0.41437244624|Raw VWC|0.5297062938712509|mS/cm
第一行:主题(行)的值,第二行:值(行)(mqtt-topics的值)
但这是500 ++的传感器... SMARTBASE并不总是SMARTBASE,因此regexp _...不是一个好主意...最后应将其保存为视图。
那有可能吗?我不知道如何实现它或如何开始。要转换表中的一行,我可以使用数据透视功能,但其余的我不知道。
我的主要问题:如何访问主题的各个值?
答案 0 :(得分:2)
使用REGEXP_SUBSTR
获取topic
列的子字符串组件,然后使用PIVOT
:
Oracle设置:
CREATE TABLE table_name ( topic, value ) AS
SELECT 'SMARTBASE/N0184/1/MOISTURE/value', '0.414' FROM DUAL UNION ALL
SELECT 'SMARTBASE/N0184/1/MOISTURE/unit', 'Raw VWC' FROM DUAL UNION ALL
SELECT 'SMARTBASE/N0184/1/MOISTURE/timestamp', '2019-01-01T00:00:00.000' FROM DUAL UNION ALL
SELECT 'SMARTBASE/N0184/1/CONDUCTIVITY/value', '0.529' FROM DUAL UNION ALL
SELECT 'SMARTBASE/N0184/1/CONDUCTIVITY/unit', 'mS/cm' FROM DUAL UNION ALL
SELECT 'SMARTBASE/N0184/1/CONDUCTIVITY/timestamp', '2019-01-01T00:00:00.000' FROM DUAL;
查询:
SELECT SENSOR_TYPE,
SENSOR,
TO_NUMBER( moisture_value ) AS moisture_value,
moisture_unit,
TO_TIMESTAMP( moisture_timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.FF3' ) AS moisture_timestamp,
TO_NUMBER( conductivity_value ) AS conductivity_value,
conductivity_unit,
TO_TIMESTAMP( conductivity_timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.FF3' ) AS conductivity_timestamp
FROM (
SELECT REGEXP_SUBSTR( topic, '[^/]+', 1, 1 ) AS sensor_type,
REGEXP_SUBSTR( topic, '[^/]+', 1, 2 ) AS sensor,
REGEXP_SUBSTR( topic, '[^/]+', 1, 4 ) AS measurement_name,
REGEXP_SUBSTR( topic, '[^/]+', 1, 5 ) AS measurement_metadata_type,
value
FROM table_name
)
PIVOT(
MAX( value )
FOR ( measurement_name, measurement_metadata_type )
IN (
( 'MOISTURE', 'value' ) AS MOISTURE_value,
( 'MOISTURE', 'unit' ) AS MOISTURE_unit,
( 'MOISTURE', 'timestamp' ) AS MOISTURE_timestamp,
( 'CONDUCTIVITY', 'value' ) AS CONDUCTIVITY_value,
( 'CONDUCTIVITY', 'unit' ) AS CONDUCTIVITY_unit,
( 'CONDUCTIVITY', 'timestamp' ) AS CONDUCTIVITY_timestamp
)
)
输出:
SENSOR_TYPE | SENSOR | MOISTURE_VALUE | MOISTURE_UNIT | MOISTURE_TIMESTAMP | CONDUCTIVITY_VALUE | CONDUCTIVITY_UNIT | CONDUCTIVITY_TIMESTAMP :---------- | :----- | -------------: | :------------ | :------------------------------ | -----------------: | :---------------- | :------------------------------ SMARTBASE | N0184 | .414 | Raw VWC | 01-JAN-19 12.00.00.000000000 AM | .529 | mS/cm | 01-JAN-19 12.00.00.000000000 AM
db <>提琴here