获得非重复值(Oracle)

时间:2014-01-24 03:45:39

标签: sql oracle

我正在尝试一些聚合技术来获取我的数据库上的非重复值,但在我放置一些聚合技术(如ltrim)之前,我总是得到相同的结果。

我的查询:

SELECT 'Data'
       ||','||LTRIM(TO_CHAR(TRUNC(CREATE_TIME),'MM/dd/yyyy'))
       ||','||LTRIM(TO_CHAR(TRUNC(CLOSE_TIME),'MM/dd/yyyy'))
       ||','||COUNT(*)
FROM APP_ACCOUNT.OTRS_TICKET
WHERE CREATE_TIME BETWEEN SYSDATE -7 AND SYSDATE
AND CLOSE_TIME BETWEEN SYSDATE -7 AND SYSDATE
GROUP BY TRUNC(CREATE_TIME), TRUNC(CLOSE_TIME)
ORDER BY TRUNC(CREATE_TIME), TRUNC(CLOSE_TIME);

以下是我总能得到的结果:

Data,01/17/2014,01/17/2014,16
Data,01/17/2014,01/20/2014,1
Data,01/17/2014,01/21/2014,2
Data,01/18/2014,01/18/2014,2
Data,01/19/2014,01/19/2014,1
Data,01/19/2014,01/20/2014,1
Data,01/19/2014,01/21/2014,1
Data,01/20/2014,01/20/2014,6
Data,01/20/2014,01/21/2014,2
Data,01/20/2014,01/22/2014,1
Data,01/21/2014,01/21/2014,13
Data,01/21/2014,01/22/2014,1

而不是(这是我真正需要的结果)

Data,01/17/2014,01/20/2014,1
Data,01/18/2014,01/18/2014,2
Data,01/19/2014,01/21/2014,1
Data,01/20/2014,01/22/2014,1
Data,01/21/2014,01/22/2014,1

表格结构。

CREATE_TIME (DATE)
CLOSE_TIME  (DATE) 

2 个答案:

答案 0 :(得分:1)

SELECT 'Data'
||','||LTRIM(TO_CHAR(TRUNC(CREATE_TIME),'MM/dd/yyyy'))
||','||LTRIM(TO_CHAR(TRUNC(MAX(CLOSE_TIME)),'MM/dd/yyyy'))
||','||COUNT(TICKETS_CLOSED)
FROM APP_ACCOUNT.OTRS_TICKET
WHERE CREATE_TIME BETWEEN SYSDATE -7 AND SYSDATE
AND CLOSE_TIME BETWEEN SYSDATE -7 AND SYSDATE
GROUP BY TRUNC(CREATE_TIME)
ORDER BY TRUNC(CREATE_TIME)

答案 1 :(得分:0)

您似乎希望按CREATE_TIME进行分组,并获取该日期的最新CLOSE_TIME。尝试从group by语句中删除CLOSE_TIME,然后选择MAX(CLOSE_TIME)。