Last record of Join table

后端 未结 7 967
忘了有多久
忘了有多久 2021-01-06 16:05

I am lookign for the correct SQL code to join 2 tables and show only the last record of the details table.

I have a DB with 2 tables,

Deals 
   Deal         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 16:54

    Not very elegant, but works in Oracle :

    select dealid,
           dealname,
           dealdetails,
           comment,
    from
    (
      select a.dealid,
             a.dealname,
             a.dealdetails,
             b.commenttime,
             b.comment,
             max(commenttime) over (partition by a.dealid) as maxCommentTime
      from deals a inner join dealcomments b on b.dealid = a.dealid
    )
    where comment = maxCommentTime
    

提交回复
热议问题