我在连接上获得重复记录,我只想在NSANOTES.NOTEDATE上使用MAX(最新)日期。你好吗?
SELECT NSAACTIVITY.NSAID ,
NSAACTIVITY.NSASERVICEIMPACTED "Type",
NSAACTIVITY.NSAAPPROVED,
NSAACTIVITY.NSADESCRIPTION,
NSANOTES.NSANOTE
FROM NSAUSER.NSAACTIVITY INNER JOIN NSAUSER.NSANOTES ON NSAACTIVITY.NSAID=NSANOTES.NSAID
WHERE TO_CHAR(NSAACTIVITY.NSASTART, 'yyyy-mm-dd hh24:mi') BETWEEN '2013-06-28 10:59:59' AND '2013-07-01 06:00:01'
答案 0 :(得分:2)
我认为你正在寻找下面的内容。我把关于在WHERE子句中使用timestamp而不是to_char的线索。休息仅仅是使用RANK()函数。希望这有帮助
SELECT *
FROM (SELECT act.NSAID,
act.NSASERVICEIMPACTED "Type",
act.NSAAPPROVED,
act.NSADESCRIPTION,
notes.NSANOTE,
rank()
OVER (PARTITION BY act.NSAID ORDER BY notes.NOTEDATE DESC) rn
FROM NSAUSER.NSAACTIVITY act
INNER JOIN
NSAUSER.NSANOTES notes
ON act.NSAID = notes.NSAID
WHERE act.NSASTART BETWEEN TIMESTAMP '2013-06-28 10:59:59' AND TIMESTAMP '2013-07-01 06:00:01'
)
where rn=1;
答案 1 :(得分:1)
您是否尝试过使用群组? 类似的东西:
SELECT NSAACTIVITY.NSAID ,
NSAACTIVITY.NSASERVICEIMPACTED "Type",
NSAACTIVITY.NSAAPPROVED,
NSAACTIVITY.NSADESCRIPTION,
NSANOTES.NSANOTE,
MAX(NSANOTES.NOTEDATE)
FROM NSAUSER.NSAACTIVITY INNER JOIN NSAUSER.NSANOTES ON NSAACTIVITY.NSAID=NSANOTES.NSAID
WHERE TO_CHAR(NSAACTIVITY.NSASTART, 'yyyy-mm-dd hh24:mi') BETWEEN '2013-06-28 10:59:59' AND '2013-07-01 06:00:01'
GROUP BY NSAACTIVITY.NSAID ,
NSAACTIVITY.NSASERVICEIMPACTED,
NSAACTIVITY.NSAAPPROVED,
NSAACTIVITY.NSADESCRIPTION,
NSANOTES.NSANOTE
答案 2 :(得分:0)
我认为这样的事情可以解决问题(使用子查询)
SELECT NSAACTIVITY.NSAID ,
NSAACTIVITY.NSASERVICEIMPACTED "Type",
NSAACTIVITY.NSAAPPROVED,
NSAACTIVITY.NSADESCRIPTION,
ns.NSANOTE
FROM NSAUSER.NSAACTIVITY INNER JOIN NSAUSER.NSANOTES ns ON NSAACTIVITY.NSAID=ns.NSAID
WHERE TO_CHAR(NSAACTIVITY.NSASTART, 'yyyy-mm-dd hh24:mi') BETWEEN '2013-06-28 10:59:59' AND '2013-07-01 06:00:01'
and ns.NOTESDATE= (SELECT MAX(ns2.NOTESDATE) FROM NSAUSER.NSANOTES ns2 where ns2.NSAID = ns.NSAID)