Link to home
Start Free TrialLog in
Avatar of jweissdandm
jweissdandm

asked on

SQL SELECT DISTINCT with multuiple columns

I need to grab distinct email address and corresponding fName,lname from my table

I need something like this...

SELECT DISTINCT value1, value2 FROM table

Thanks
Avatar of Aneesh
Aneesh
Flag of Canada image

select emailaddress, fname, lname
from
(select emailaddress, fname, lname, rn = row_number() over( partition by emailaddress order by fname,lname )  from yourtable  ) a where a.rn = 1
SELECT      DISTINCT emailaddress,
            (SELECT     TOP (1) fname
                  FROM          table
            WHERE      (emailaddress= T.emailaddress)) AS FName,
            (SELECT     TOP (1) lname
            FROM          table
                  WHERE      (emailaddress= T.emailaddress)) AS LName
FROM         table AS T
GROUP BY emailaddress
Avatar of jweissdandm
jweissdandm

ASKER

would this work?

select value1,max(value2) as newColumn
from table1
group by value1
Yes. It would work with GROUP BY and AGGREGATE function as well.
ASKER CERTIFIED SOLUTION
Avatar of lof
lof
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the explanation homie