vendredi 13 novembre 2015

How do I remove duplicate records and only take the one with the higest revision number

I would like to only have one record returned for each VariableName, and it needs to be the highest revision number. I have tried GROUP BY and DISTINCT, but nothing seems to work.

I have this SQL Code

    SELECT Docs.DocID
      ,Filename
      ,VName
      ,VText
      ,RevNo
  FROM Docs
  INNER JOIN VarVal ON Docs.DocID = VarVal.DocID
  INNER JOIN Vars ON Vars.VarID = VarVal.VarID
  WHERE Docs.DocID in (
                                    select DocID
                                    from Docs
                                    INNER JOIN VarVal on Docs.DocID = VarVal.DocID
                                    INNER JOIN Var on Var.VarID = VarVal.VarID
                                    where VarName = '07'
                                    and ValueText = '801016'
                                )
  AND Docs.Deleted = 0
  AND Var.IsDeleted = 0
  ORDER BY VName

That produces this result this is the query result

Aucun commentaire:

Enregistrer un commentaire