SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Filtering data by getting only distinct columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Apples
Posting Yak Master

146 Posts

Posted - 01/11/2013 :  15:48:15  Show Profile  Reply with Quote
I have a query

SELECT *
FROM tblMyTable

that returns these results:

ID | TypeID | Name
----------------------------------
1 | 1 | AAA
2 | 1 | BBB
3 | 2 | CCC
4 | 4 | DDD
5 | 5 | EEE
6 | 5 | FFF
7 | 5 | GGG

I'd like to filter the results so that there are only distinct TypeIds, so that the results returned are:

ID | TypeID | Name
----------------------------------
1 | 1 | AAA
3 | 2 | CCC
4 | 4 | DDD
5 | 5 | EEE

How can I change my query to do that?

adry_fab
Starting Member

1 Posts

Posted - 01/11/2013 :  16:38:36  Show Profile  Reply with Quote
SELECT
MIN(ID),
TypeID,
min(Name)
FROM tblMyTable
GROUP BY TypeID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 01/12/2013 :  03:27:31  Show Profile  Reply with Quote

SELECT ID,TypeID,Name
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY TypeID ORDER BY ID) AS Seq,*
FROM table
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000