英文:
Systimestamp In oracle
问题
以下是翻译好的部分:
我正在使用以下的SQL语句。下面的SYSTIMESTAMP在所有记录中显示相同的秒数值。这个表包含一百万条记录。我们如何在每条记录中显示秒数或纳秒的变化?还有其他方法可以区分每条记录的时间秒数吗?
SELECT ITEM, ITEM_NO, DESCRIPTON, SYSTIMESTAMP FROM ITEM_MASTER ORDER BY ITEM_NO;
英文:
I am using below SQL statement. The below systimestamp displays the same seconds values for all the records. The table contains one million records. How can we displays the seconds or nano seconds variations for each records? OR any other methods to differentiate the seconds in time for each record?
SELECT ITEM,ITEM_NO,DESCRIPTON,SYSTIMESTAMP FROM ITEM_MASTER ORDER BY ITEM_NO;
答案1
得分: 1
SYSTIMESTAMP
返回所有行相同的时间;它不会返回每行从数据文件中读取或放入结果集的时间。因此,您提出的要求是不可能实现的。
如果您想要为行的顺序在结果集中的顺序生成唯一标识符,可以使用 ROWNUM
伪列:
SELECT ITEM,
ITEM_NO,
DESCRIPTON,
ROWNUM
FROM ITEM_MASTER
ORDER BY ITEM_NO;
如果您希望行按与输出相同的顺序编号,首先进行排序,然后应用 ROWNUM
:
SELECT t.*,
ROWNUM
FROM (
SELECT ITEM,
ITEM_NO,
DESCRIPTON
FROM ITEM_MASTER
ORDER BY ITEM_NO
) t;
或者使用 ROW_NUMBER
分析函数:
SELECT ITEM,
ITEM_NO,
DESCRIPTON,
ROW_NUMBER() OVER (ORDER BY item_no) AS rn
FROM ITEM_MASTER
ORDER BY ITEM_NO;
如果您希望将其转换为一个时间戳,该时间戳每行递增一微秒,可以使用以下查询:
SELECT ITEM,
ITEM_NO,
DESCRIPTON,
SYSTIMESTAMP + ROWNUM * INTERVAL '0.000001' SECOND AS rn_time
FROM ITEM_MASTER
ORDER BY ITEM_NO;
实际上,我正在尝试以与选择语句返回的顺序相同的方式更新记录。
这似乎是一个 XY 问题;您不需要使用时间,而且没有 ORDER BY
子句的情况下不能保证结果集的顺序,因此如果您希望按 ITEM_NO
顺序使用数据,那么您已经有一个可用于排序行的现有列,无需人为生成一个“时间”列。
英文:
SYSTIMESTAMP
returns the same time for all rows; it does not return the time each row was read from a data file or put into the result set. Therefore what you are asking is impossible.
If you want to have a unique identifier for the order that the rows were put into the result set then use the ROWNUM
pseudo-column:
SELECT ITEM,
ITEM_NO,
DESCRIPTON,
ROWNUM
FROM ITEM_MASTER
ORDER BY ITEM_NO;
If you want the rows to be numbered in the same order as the output then order first and then apply ROWNUM
:
SELECT t.*,
ROWNUM
FROM (
SELECT ITEM,
ITEM_NO,
DESCRIPTON
FROM ITEM_MASTER
ORDER BY ITEM_NO
) t;
or use the ROW_NUMBER
analytic function:
SELECT ITEM,
ITEM_NO,
DESCRIPTON,
ROW_NUMBER() OVER (ORDER BY item_no) AS rn
FROM ITEM_MASTER
ORDER BY ITEM_NO;
If you want to convert that to a timestamp that is artificially incremented by a micro-second for each row then:
SELECT ITEM,
ITEM_NO,
DESCRIPTON,
SYSTIMESTAMP + ROWNUM * INTERVAL '0.000001' SECOND AS rn_time
FROM ITEM_MASTER
ORDER BY ITEM_NO;
> actually i am trying to update the records in the same order how the select statement returns.
This appears to be an XY-problem; you do not need to use a time and the order of a result set is not guaranteed without an ORDER BY
clause so if you want to use the ITEM_NO
order then you already have a pre-existing column you can use to order the rows and do not need to artificially generate a "time" column.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论