I have this query :
SELECT pih.PID, piv.TSV, piv.Variable_ID, piv.Value_ID, piv.Manual_Value
FROM Process_Instance_Value AS piv
INNER JOIN Process_Instance_History AS pih ON piv.PID = pih.PID
WHERE piv.Variable_ID IN ( SELECT Variable_ID
FROM someTable
WHERE Transition_ID = @someValue)
AND piv.PID = @someValue
I get this :

My problem is that those rows are duplicate for the use I want them at this point.
Is it possible to only get rows on DISTINCT TSV? Or something like that?
Thank you
You can do this:
Note that: This will return the minimum
pih.PIDfor each distinctpiv.TSVif you want the max, just useORDER BY pih.PID DESCinstead.How does this work:
The ranking function
ROW_NUMBER() OVER()will give a ranking number for each group that has the same column listed in thePARTITION BY, in our casepiv.TSV, this ranking number will be ordered, for each group, according to theORDER BYclause defined inside theOVERclause, in our caseORDER BY pih.PID. I used a CTE instead of using subquery.Then the I used
WHERE rownum = 1, since the ranking numberrownumwas given for each group of records that has the samepiv.TSVranked in descendent order, then the record withrownum = 1will be the record with the minimumpih.PID(think about it).You can however, get the same result using an inner join with a
GROUP BYlike so: