第一次问有关stackoverflow的问题,因此对任何错误表示歉意。
我正在尝试将旧表转换为新格式。
旧表/输入示例:
| id | collectionId | infoText |
|----------------------------------|----------------------------------|------------|
| 20200227112631476162094432822589 | 20200227112630931296846572143651 | Step 0 |
| 20200227112631512664092998338570 | 20200227112630931296846572143651 | Step 1 |
| 20200227112631652576662844108316 | 20200227112630931296846572143651 | Successful |
新表/输出:
| collectionId | startTimestamp | stopTimeStamp | lastStatus |
|----------------------------------|---------------------------------|---------------------------------|-------------|
| 20200227112630931296846572143651 | 27-FEB-20 11.26.30.931000000 AM | 27-FEB-20 11.26.50.911000000 AM | Successful |
基本上,以下是必需的:
我整天都在尝试这样做,我觉得我即将解决它。但是,似乎我尝试的所有操作都会导致不同的错误。
我最近的尝试:
CREATE table newTable AS SELECT a.collectionId AS collectionId, a.id AS id, to_timestamp(substr(a.collectionId , 0, 17), 'YYYYMMDDHH24MISSFF') AS starttimestamp, "STOPTIMESTAMP" AS stoptimestamp, a.infoText AS lastStatus, FROM oldTable a INNER JOIN ( SELECT MAX(id), to_timestamp(substr(MAX(id), 0, 17), 'YYYYMMDDHH24MISSFF') AS stoptimestamp, collectionId AS collectionId FROM oldTable GROUP BY collectionId ) b ON a.collectionId = b.collectionId AND stoptimestamp = b.stoptimestamp;
但是这会导致具有重复的集合ID的表。
我非常感谢您的帮助,因为我对SQL的经验还不多。此处显示的示例已修改为更简单,我正在使用的表具有更多(超文本)字段,并且包含超过2M +行。如果有帮助,它是Oracle XE 18c DB。
感谢您的帮助!
第一次问有关stackoverflow的问题,因此对任何错误表示歉意。我正在尝试将旧表转换为新格式。旧表/输入示例:| id | ...
您可以使用窗口功能来识别每个组的最后一条记录,然后进行日期转换:
select
collection_id,
to_timestamp(substr(collection_id, 1, 17), 'yyyymmddhh24missff') start_timestamp,
to_timestamp(substr(id, 1, 17), 'yyyymmddhh24missff') end_timestamp,
info_text last_status
from (
select
t.*,
row_number() over(partition by collection_id order by id desc) rn
from mytable t
) t
where rn = 1