SELECT cust_detl.*,
MIN(CREATION_TIMESTAMP) OVER (PARTITION BY CUST_ID) AS MIN_TIMESTAMP
FROM CUST_DETAILS cust_detl
WHERE CREATION_TIMESTAMP=MIN_TIMESTAMP;
以上查询选择表CUST_DETAILS
中CREATION_TIMESTAMP
列中包含最旧值的所有列。
知道为什么MIN_TIMESTAMP
会遇到无效的标识符?
这些是应显示的列:
SELECT
CUSTOMER_DTL_SEQ.nextval,
CUST_ID
CUS_REF_ID
CUST_NAME
CUST_ADDRESS
CREATION_TIMESTAMP
FROM
(
CUSTOMER_DTL_SEQ.nextval,
cust_detl.CUST_ID,
cust_detl.CUST_REF_ID,
cust_detl.CUST_NAME,
cust_detl.CUST_ADDRESS,
cust_detl.CREATION_TIMESTAMP,
MIN(CREATION_TIMESTAMP) OVER (PARTITION BY CUST_ID) AS min_timestamp
FROM cust_details cust_detl
)
WHERE CREATION_TIMESTAMP = min_timestamp;
我还需要选择CREATION_TIMESTAMP
列,只会选择那些时间戳最小的列。问题是不允许使用nextval
的序列。我需要在查询中使用序列,因为此语句稍后会用于INSERT
SELECT...INSERT INTO
PK需要递增。
答案 0 :(得分:1)
列名尚未生效,首先使用where条件过滤数据,然后在过滤后的数据上过滤,select语句有效。您需要先将其放在子查询中才能使用它。
SELECT * FROM
(SELECT cust_detl.*,
MIN(CREATION_TIMESTAMP) OVER (PARTITION BY CUST_ID) AS MIN_TIMESTAMP
FROM CUST_DETAILS cust_detl)
WHERE CREATION_TIMESTAMP=MIN_TIMESTAMP;
更新:我不知道您的表中有哪些列列表,但如果您只需要特定列,那么查询就是这样(假设您只需要列cust_id,column1 ,选择列表中的column2和column3)
SELECT cust_id,
column1,
column2,
column3
FROM (SELECT cust_detl.cust_id,
cust_detl.column1,
cust_detl.column2,
cust_detl.column3,
cust_detl.creation_timestamp,
MIN(creation_timestamp) over(PARTITION BY cust_id) AS min_timestamp
FROM cust_details cust_detl)
WHERE creation_timestamp = min_timestamp;
如果您仍然没有得到解决方案,请发布表格中的列列表和预期的输出。
Update2 :在外部查询中获取光标,此查询应该可以正常工作。
SELECT customer_dtl_seq.nextval,
cust_id,
cus_ref_id,
cust_name,
cust_address,
creation_timestamp
FROM (SELECT cust_detl.cust_id,
cust_detl.cust_ref_id,
cust_detl.cust_name,
cust_detl.cust_address,
cust_detl.creation_timestamp,
MIN(creation_timestamp) over(PARTITION BY cust_id) AS min_timestamp
FROM cust_details cust_detl)
WHERE creation_timestamp = min_timestamp;