开发者

Get Latest Version of a document SQL

I have a problem with the following SQL

SELECT c1.* FROM rmanager.requisitos_nao_funcionais c1 
left join rmanager.requisitos_nao_funcionais c2 
on c1.document_id = c2.document_id and c1.versao < c2.versao
where c1.id_projeto = 15 
and c1.id_req_fun = 5
and c2.document_id is null 
order by ordem ASC;

I've tried by a subselect also

SELECT *
FROM rmanager.requisitos_nao_funcionais A
WHERE versao开发者_如何学JAVA = (
    SELECT MAX(versao)
    FROM rmanager.requisitos_nao_funcionais B
    WHERE B.document_id = A.document_id
)
and id_req_fun = 5
order by ordem asc;

but both returns me

Get Latest Version of a document SQL

it doesn't get the document_id 1, it should return me 1,2,3 getting the lastest version of them

The table data is below

Get Latest Version of a document SQL

The SQL ideia is to get the lastest version of a document_id group(each document_id can has many versions) from a id_project and id_req_fun

I can't find any problem.


  SELECT c1.document_id, MAX(c1.version)
    FROM rmanager.requisitos_nao_funcionais as c1
   WHERE c1.id_projecto = 15
     AND c1.id_req_fun = 5
GROUP BY c1.document_id

I am not sure if i understood properly, but given a project and a req_fun this will give you the latest version for each document in the set.

Now if you want the document (a single one) with the biggest version number try:

  SELECT c1.document_id, c1.version
    FROM rmanager.requisitos_nao_funcionais as c1
   WHERE c1.id_projecto = 15
     AND c1.id_req_fun = 5
ORDER BY c1.version DESC
   LIMIT 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜