简体   繁体   中英

Oracle SQL Select Date Time with Earliest Time

I have a query that returns this

在此处输入图片说明

And I would like to to return this, where earliest hour of the same day is chosen

在此处输入图片说明

This is my query so far --- MIN(I.CREATIONDATE) does return the date-time format I want, I was hoping that MIN would select the earliest time.

   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)

Use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM