We have a requirement like fetching all the columns of a table by filtering distinct values of one column along with the max value of another column.
Example:
Table A
id name qty
__ __ __
1 Dinesh 10
2 Ramesh 8
3 Dinesh 2
4 Ganesh 3
5 Ganesh 20
Here fetching distinct name along with max(qty);
Expected output:
1 Dinesh 10
2 Ramesh 8
5 Ganesh 20
Can anyone say how to achieve this in DB2 query?
Thanks in advance
You could use a correlated sub-query to do this.
However, note that if there is another record with the same name and maximum qty (such as
6 Dinesh 10) then both records will be returned. This can be resolved be writing the query slightly differently: