Oracle SQL选择日期时间最早的时间

时间:2019-05-14 13:03:02

标签: sql oracle datetime group-by min

我有一个返回此查询的查询

enter image description here

我想返回这个,选择当天的最早时间

enter image description here

到目前为止,这是我的查询--- MIN(I.CREATIONDATE)确实返回了我想要的日期时间格式,我希望MIN将选择最早的时间。

   SELECT TO_CHAR(MIN(I.INCIDENTID))               AS "Incident ID",
          MIN(I.CREATIONDATE)                      AS "Creation Date",
          TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY')     AS "Date",
          TRIM(MO.DOMAINUSERNAME)                  AS "Login ID",  
          TRIM(M.MESSAGESUBJECT)                   AS "Email Subject"
   FROM   MESSAGE M 
   JOIN   INCIDENT I 
   ON     M.MESSAGESOURCE = I.MESSAGESOURCE 
   AND    M.MESSAGEID = I.MESSAGEID
   AND    M.MESSAGEDATE = I.MESSAGEDATE 
   JOIN   MESSAGEORIGINATOR MO
   ON     M.MESSAGEORIGINATORID = MO.MESSAGEORIGINATORID                   
   GROUP BY TO_CHAR(I.CREATIONDATE,'MM-DD-YYYY'),
            TRIM(MO.DOMAINUSERNAME),
            TRIM(M.MESSAGESUBJECT)

1 个答案:

答案 0 :(得分:1)

使用row_number()

with CTE as
(
select t1.*, 
       row_number() over (partition by trunc(creation_date) order by creation_date) rn
from Mytable t1
)
select *
from CTE
where rn = 1