Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to keep unique item?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-11-20 : 09:26:38
Hi there,

See the following case: table1 has columns of Code, Model ... now the data as:

Code | Model | ...
ABC | N1
BBC | N2
XYX | N1
TTY | N3
HJY | N2
TTH | N4
....


Now I need select code with only each unique model N1, N2 ... once. For ABC and XYX both have N1 model, but only one is required. How can I do that? Thanks in advance.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-11-20 : 09:58:31
This will give unique Models with arbitrary code

SELECT t1.Code,t1.Model
FROM
(
SELECT Code,Model, [SingleValue] = ROW_NUMBER() OVER(PARTITION BY model ORDER BY code)
FROM yourTable
) t1

WHERE t1.SingleValue = 1

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-11-20 : 10:44:32
Beautiful code! It works perfect. Now I know a function can be returned to a variable and later the variable is used in WHERE clause. It works perfect, thank you so much Jim. Have a wonderful Thanksgiving!!!
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-11-20 : 11:08:39
You're very welcome. Happy Thanksgiving to you, too.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -