Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
SQL
ce
clients
clustering
connect
datamining
datawarehouse
dts
fulltext
jdbcdriver
msde
mseq
newusers
notificationsvcs
odbc
olap
programming
replication
reportingsvcs
security
securitytools
server
setup
sqlxml.viewmapper
tools
xml
  
 
date: Tue, 2 Sep 2008 07:38:00 -0700,    group: microsoft.public.sqlserver.server        back       


Help with SELECT DISTINCT   
Hello,

I have a table called Products with columns ID (Primary key), Names, 
Category, Store and Vendor. I want to get the unique names along with their 
ID that fall under Category T, so I tried the following,

SELECT DISTINCT Names, ID
FROM         Products
WHERE     (Category = 'T')

But it doesn’t seem to work.  Then I tried SELECT statement with 
DISTINCT(Names), but it didn’t work

It works if I removed ID column from the SELECT statement. Any idea what is 
wrong with the above query?

How do I run Distinct on one column but return 2 columns?

Thank you,

Joe
date: Tue, 2 Sep 2008 07:38:00 -0700   author:   Joe

Re: Help with SELECT DISTINCT   
If you have multiple IDs for the same name, what do you want to return? 
Here is one way:

SELECT Names, MAX(ID)
FROM Products
WHERE Category = 'T'
GROUP BY Names;

You can replace MAX with MIN if you want to retrieve the MIN id.

-- 
Plamen Ratchev
http://www.SQLStudio.com
date: Tue, 02 Sep 2008 11:15:27 -0400   author:   Plamen Ratchev

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us