SQL query to sort the ID as ASC and Zip must not dup

54 Views Asked by At

I have an SQL statement like this:

SELECT         TOP 100 id, 
               lastname, 
               firstname, 
               address1, 
               city, 
               state, 
               zip 
FROM   leads 
WHERE  id > 100 
ORDER  BY id ASC 

Now I want the ZIP to be different (not dup) for 100 result with 1 query statement like that.

1

There are 1 best solutions below

0
On BEST ANSWER
SELECT         TOP 100 l.id, 
               l.lastname, 
               l.firstname, 
               l.address1, 
               l.city, 
               l.state, 
               l.zip 
FROM   leads l
WHERE   l.id = (select MIN (id) FROM leads l2 where l2.zip=l.zip)
ORDER  BY l.id ASC