0

我在加入时收到重复记录,我只想要 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'
4

3 回答 3

2

我认为您正在寻找以下内容。我得到了关于在 WHERE 子句中使用时间戳而不是 to_char 的线索。Rest 只是对 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;
于 2013-06-28T16:14:52.203 回答
1

您是否尝试使用 group by ?就像是:

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 
于 2013-06-28T15:17:25.100 回答
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)
于 2013-06-28T15:17:43.623 回答