Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trying to limit the number of rows returned from joined table.

Jag108Mar 6 2016 — edited Mar 6 2016

Hi There,

I am trying to retreive the most recent dated record from two joined tables. One table has the unique record that I am looking for, the second has seperate log entries that are related to the single record.

What I am trying to do is return the most recent dated log record to match the original record, but as soon as I add more than one column from the "log" table all the historical log records are returned, not wanted, we just want to see the most recent log entry, and when it was entered.

QUERY:

SELECT WO.WONUM

      ,WO.STATUSDATE

      ,WO.ACTSTART

      ,WO.DESCRIPTION

      ,WO.LOCATION

      ,WO.ASSETNUM

      ,WO.WORKTYPE

      ,WO.FAILURECODE

      ,WO.PROBLEMCODE

      ,WO.SUPERVISOR

      ,WO.CREWID

      ,WO.WOPRIORITY

      ,W.LOGTYPE

      ,W.DESCRIPTION

      ,W.CREATEDATE

FROM WORKORDER WO

      INNER JOIN(SELECT MAX(wl.createdate)

                 ,WL.RECORDKEY

                 ,WL.LOGTYPE

                 ,WL.DESCRIPTION

                 ,WL.CLASS

           FROM WORKLOG WL

            group by wl.recordkey

                     ,wl.logtype

                     ,WL.DESCRIPTION

                     ,WL.CLASS) W ON (W.RECORDKEY = WO.WONUM AND W.CLASS = 'WORKORDER')

WHERE WO.STATUS IN ('INPRG','WAPPR','APPR','WINFO')

AND WO.WONUM LIKE '3393278' -- selecting a sample record that has many log entries for testing

AND WO.WORKTYPE IN ('RM','RW')

I would like to add columns from the WORKLOG table to the output, but there is a many to one relationship between the WORKLOG table and the WORKORDERS table.

This post has been answered by Paulzip on Mar 6 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2016
Added on Mar 6 2016
3 comments
1,322 views