SQL与Distinct和Date

时间:2015-01-28 15:42:33

标签: sql date distinct

我有以下SQL语句:

select DISTINCT 
    USERID, 
    NAME, 
    SURNAME, 
    RESIDENCE, 
    HIT_DATE 
from PAGE_HIT 
where HIT_DATE < add_months(sysdate, -6);

我的问题是我多次获取记录,因为HIT_DATE不同。例如,结果是:

editore, editor, eddy, blabla, 28.01.2013

editore, editor, eddy, blabla, 26.01.2013

editore, editor, eddy, blabla, 14.01.2013

editore, editor, eddy, blabla, 03.01.2013

但我只是希望获得最新HIT_DATE的条目,在此示例中为:

editore, editor, eddy, blabla, 28.01.2013

我不知道如何修改我的SQL语句

2 个答案:

答案 0 :(得分:1)

这样:

select USERID, NAME, SURNAME, RESIDENCE, max(HIT_DATE) from PAGE_HIT where HIT_DATE < add_months(sysdate, -6)
GROUP BY USERID, NAME, SURNAME, RESIDENCE;

答案 1 :(得分:0)

使用GROUP BY

select
            USERID,
            NAME,
            SURNAME,
            RESIDENCE,
            max(HIT_DATE)
    from
            PAGE_HIT
    where
            HIT_DATE < add_months(sysdate, -6)
    group by
            USERID,
            NAME,
            SURNAME,
            RESIDENCE;