我有下面的代码,给我这个错误
错误:“YYYY”的值“test”无效
代码:
select * from
(select i.uuid as t1uid,
m3.short_id as short_id_start,
m.text_value as original_date,
TO_TIMESTAMP( trim ( substring(m.text_value, 1, 19)), 'YYYY-MM-DD"T"HH24:MI:SS' ) as tms
from item i
inner join metadatavalue m on i.uuid = m.dspace_object_id
inner join metadatafieldregistry m2 on m.metadata_field_id = m2.metadata_field_id
inner join metadataschemaregistry m3 on m2.metadata_schema_id = m3.metadata_schema_id
and m3.short_id ='interaction'
and m2."element" = 'startts') t1
join
(select i.uuid as t2uid,
m3.short_id as short_id_fail,
m.text_value as success_status
from item i
inner join metadatavalue m on i.uuid = m.dspace_object_id
inner join metadatafieldregistry m2 on m.metadata_field_id = m2.metadata_field_id
inner join metadataschemaregistry m3 on m2.metadata_schema_id = m3.metadata_schema_id
and m3.short_id ='interaction'
and m2."element" = 'status'
and m.text_value = 'SUCCESS') t2 on t1.t1uid = t2.t2uid
join
(select i.uuid as t3uid,
m3.short_id as short_id_start_endt,
TO_TIMESTAMP( trim ( substring(m.text_value, 1, 19)), 'YYYY-MM-DD"T"HH24:MI:SS' )as enddate
from item i
inner join metadatavalue m on i.uuid = m.dspace_object_id
inner join metadatafieldregistry m2 on m.metadata_field_id = m2.metadata_field_id
inner join metadataschemaregistry m3 on m2.metadata_schema_id = m3.metadata_schema_id
and m3.short_id ='interaction'
and m2."element" = 'endts') t3 on t3.t3uid = t2.t2uid
left join
(select m.text_value as "Protocollo",
i.uuid as t4uid
from item i
inner join metadatavalue m on i.uuid = m.dspace_object_id
inner join metadatafieldregistry m2 on m.metadata_field_id = m2.metadata_field_id
inner join metadataschemaregistry m3 on m2.metadata_schema_id = m3.metadata_schema_id
where m3.short_id ='interaction'
and m2."element" = 'protocol' ) t4 on t4.t4uid = t3.t3uid
left join
(select i.uuid as t5uid,
m2.qualifier as qualifier_sender,
m.text_value as "Sender"
from item i
inner join metadatavalue m on i.uuid = m.dspace_object_id
inner join metadatafieldregistry m2 on m.metadata_field_id = m2.metadata_field_id
inner join metadataschemaregistry m3 on m2.metadata_schema_id = m3.metadata_schema_id
where m3.short_id ='interaction'
and m2."element" = 'application'
and m2.qualifier in ('sender') ) as t5 on t1.t1uid = t5.t5uid
left join
(select i.uuid as t6uid,
m2.qualifier as qualifier_receiver,
m.text_value as "Receiver"
from item i
inner join metadatavalue m on i.uuid = m.dspace_object_id
inner join metadatafieldregistry m2 on m.metadata_field_id = m2.metadata_field_id
inner join metadataschemaregistry m3 on m2.metadata_schema_id = m3.metadata_schema_id
where m3.short_id ='interaction'
and m2."element" = 'application'
and m2.qualifier in ('receiver') )
as t6 on t1.t1uid = t6.t6uid
where tms > TO_TIMESTAMP('2023-05-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
最后一行:where tms > TO_TIMESTAMP('2023-05-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
是问题所在(没有它代码也能正常工作)
但当我这样做:
create table public.timeline_success as
...
same code of before without the last where condition.
select * from public.timeline_fail t1
where t1.tms > TO_TIMESTAMP('2023-05-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
密码有效如何解决这一问题?
1条答案
按热度按时间w7t8yxp51#
最后一行
where tms > TO_TIMESTAMP('2023-05-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
不是问题的原因,而是暴露问题的触发条件。如果没有这一行,从t1
引用的唯一列是t1uid
,并且只需要连接到其他表。PostgreSQL跳过对t1
的选择列表的其余部分进行求值,因为这些值不会对最终结果集产生影响。当包含最后一个条件时,则必须计算tms
。通过修改有问题的数据或修改查询以妥善处理列中的无效数据来解决问题。
下面演示了所描述的行为:
运行上述返回:
| t1id| TMS| t2id|试验标号|
| - -----|- -----|- -----|- -----|
| 2| 2019 -05-27 02:00:00-04| 2|通过|
在未注解
WHERE
子句的情况下运行会导致以下异常:以下来自
EXPLAIN (ANALYZE, VERBOSE)
的输出提供了一些关于正在发生的事情的见解。请注意,原始列值是从顺序扫描返回的。对这些值的操作只有在需要时才会进行计算。优雅地处理具有无效时间戳格式的文本的一种方法是创建一个函数,该函数返回成功转换的值,如果发生异常则返回
NULL
。下面的函数支持这种方法。将对
TO_TIMESTAMP
的调用替换为对try_to_timestamp
的调用后,当遇到无效的时间戳字符串时,查询不再失败。| t1id| TMS| t2id|试验标号|
| - -----|- -----|- -----|- -----|
| 2| 2019 -05-27 02:00:00-04| 2|通过|