How to Apply DISTINCT condition on only one Column

Problem:- Lot of time we need to apply Distinct on only one column, but the DISTINCT keyword doesn't provided by SQL applies distinct to whole row.

Solution:- The following query can solve the problem.


Select * from TestTable T where Testno IN(Select MAX(Testno) from TestTable where T.Testname=Testname)

In this query Testno is the Testname is the column by which you want to distinct the rows, and Testno is the column which is not distinct (for e.g. Primary key)

No comments:

Post a Comment