Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using group by and maximum

Tags:

sql

t-sql

I have a post table and postcontent table.

Each time post is edited,an entry is added to postcontent table .

On each such time a filed called version will be incremented by 1 In the postcontent table.

How can I fetch PostId, from post able and [Description] from to postcontent table Where version for each post is maximum Ie group by post id ie i am expecting one raw for each post such that it is the 'maximum of version' for the post

like image 664
Kuttan Sujith Avatar asked Jan 23 '26 03:01

Kuttan Sujith


1 Answers

select P.PostID,
       PC.[Description]
from Post as P
inner join 
  (
    select PostID,
           [Description],
           row_number() over(partition by PostID order by [Version] desc) as rn
    from PostContent
  ) as PC
  on P.PostID = PC.PostID
where PC.rn = 1
like image 174
Mikael Eriksson Avatar answered Jan 24 '26 20:01

Mikael Eriksson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!