我有一个带有唯一ID的重复行的表,我需要编写一个只返回这些重复行的一条记录的脚本。下面代码的结果有时返回一个值,有时不返回。你能帮我理解为什么吗?
SELECT NOENV.NOENVID,
SUM(NOENV.COST),
SEQ
FROM (
SELECT ENV.NOENVID,
NOENV.COST,
ROW_NUMBER() OVER(PARTITION BY NOENV.ENVID
ORDER BY NOENV.DATE ASC, NOENV.STARTDATE ASC) AS SEQ
FROM STOCK NOENV
INNER JOIN STOCKENV ENV
ON ENV.ENVID=NOENV.ENVID
WHERE TRUNC(STARTDATE)<= TO_DATE('20180101','YYYYMMDD')
AND TRUNC(ENDDATE)> TO_DATE('20180101','YYYYMMDD')+1
) NOENV
WHERE NOENV.SEQ=1 and NOENV.NOENVID='897456'
GROUP BY SEQ, NOENV.NOENVID
示例行可以在下面找到。
NOENVID SUM(NOENV.COST) SEQ
897456 8794654,22 1
我猜测当seq!= 1代码没有返回任何行时。我有重复的行,因为数据在这些表中保持历史记录,即使数据没有变化,仍然会有当天的记录。
NOENV表中的样本数据:
DATE ENVID COST STARTDATE
20180101 897456 8794654,22 20171231
20180102 897456 8794654,22 20171231
20180103 897456 8794654,22 20171231
20180104 897457 98884654,22 20171110