好做法,感谢您发布您的问题!
这里有一种方法来做你需要的。这个问题有三个部分:首先,您需要连接两个表,以便使用实际的描述符而不是代码。其次,您需要旋转,以便将入口热电偶和气流读数分为两列。第三,如果只有一个时间戳,你只有一个空气流量值,但是入口热电偶没有值,你希望IT值填充最近的读数(值)。
该联接很简单,第三部分是IGNORE NULLS
选项的LAG()
函数的直接应用。 (注意 - 这个函数允许使用一个开窗子句;默认是rows between unbounded preceding and current row
,这正是我们需要的,所以我没有使用这个参数 - 默认是完美的。)
唯一的小故障是 - 除非你使用动态SQL - 您需要必须知道您在SQL语句中使用的每个表中的列的数量和名称。因此,在您的表格中将“入口热电偶”和“气流”描述为无效 - 在声明中它们仍然是硬编码。因此,您可以使用这种方式来编写查询,或者您需要开发动态SQL代码。
下面是代码,包括您的测试数据,然后是查询的输出。
with data_table (ts, code, val) as (
select to_timestamp('2016-05-11 13:56:47', 'yyyy-mm-dd hh24:mi:ss'),
'1000.ME', 26.9 from dual union all
select to_timestamp('2016-05-11 13:56:47', 'yyyy-mm-dd hh24:mi:ss'),
'1313.ME', 9.1136 from dual union all
select to_timestamp('2016-05-11 13:56:51', 'yyyy-mm-dd hh24:mi:ss'),
'1313.ME', 9.1233 from dual union all
select to_timestamp('2016-05-11 13:56:56', 'yyyy-mm-dd hh24:mi:ss'),
'1313.ME', 9.1136 from dual union all
select to_timestamp('2016-05-11 13:57:00', 'yyyy-mm-dd hh24:mi:ss'),
'1000.ME', 27 from dual union all
select to_timestamp('2016-05-11 13:57:00', 'yyyy-mm-dd hh24:mi:ss'),
'1313.ME', 9.1331 from dual
),
signals_table (code, descriptor) as (
select '1000.ME', 'Inflow Thermocouple' from dual union all
select '1313.ME', 'Air Flow' from dual
),
j as (
select d.ts, s.descriptor, d.val from data_table d
inner join signals_table s on d.code = s.code
),
p as (
select * from j
pivot (min(val) for descriptor in ('Inflow Thermocouple' as it,
'Air Flow' as af))
)
select ts, last_value(it ignore nulls) over (order by ts) as "Inflow Thermocouple",
last_value(af ignore nulls) over (order by ts) as "Air Flow"
from p
order by ts;
注 - 下面的输出显示了实际的时间戳数据类型;我不确定您的“timestamp”列是否实际上是DATE数据类型或真正的时间戳,查询应该以任何方式工作。另外,在数据库中使用Oracle关键字(如“timestamp”或“date”)作为表名或列名通常是一个非常糟糕的主意。我改变了ts。
TS Inflow Thermocouple Air Flow
--------------------------------------------- ------------------- ----------
11-MAY-16 01.56.47.000000000 26.9 9.1136
11-MAY-16 01.56.51.000000000 26.9 9.1233
11-MAY-16 01.56.56.000000000 26.9 9.1136
11-MAY-16 01.57.00.000000000 27 9.1331
在你想要的输出中,为什么第二排和第三排有26.900?对于那个时间戳,你对Inlet Thermpcouple没有任何价值,那么你如何选择26.900而不是27或者null? – Aleksej
您不能动态地使用信号表中的行来确定结果集中的列。 (无论如何,除非你想用XML输出)。使用固定值,您可以轻松获取该数据,但不能使用动态值。 –
@AlexPoole - 哈,我很高兴我提到过,否则我不得不编辑我的帖子。:-) – mathguy