1

I have content table where I have many contents and various versions of those contents. The columns are content varchar,document_id int (identifies a content between many versions) and version int

I want to return the lastest version of each content group. I tried using this SQL but it returns me just the first version but I want the last.The order by doesn't work with group by.

SELECT * FROM content where id_project = 8 group by document_id order by version desc;

How can i get the lastest version of all unique content(unique content is identified by the document_id)?

Thanks if anyone can help me

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
dextervip
  • 4,999
  • 16
  • 65
  • 93

3 Answers3

1

SELECT document_id,MAX(version) FROM content WHERE id_project = 8 GROUP BY document_id

chx
  • 11,270
  • 7
  • 55
  • 129
1
SELECT c1.* FROM content c1
  left join content c2 on c1.document_id = c2.document_id and c1.version < c2.version
 where c2.document_id is null
   and c1.id_project = 8 
Imre L
  • 6,159
  • 24
  • 32
0

I believe you want something like

SELECT *
FROM content A
WHERE version = (
    SELECT MAX(version)
    FROM content B
    WHERE B.document_id = A.document_id
)
AND document_id = 8

Assuming each set of versions is "grouped" by a document_id.

bhamby
  • 15,112
  • 1
  • 45
  • 66