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.
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 | N1BBC | N2XYX | N1TTY | N3HJY | N2TTH | 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 codeSELECT t1.Code,t1.ModelFROM(SELECT Code,Model, [SingleValue] = ROW_NUMBER() OVER(PARTITION BY model ORDER BY code)FROM yourTable) t1WHERE t1.SingleValue = 1JimEveryday I learn something that somebody else already knew |
|
|
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!!! |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-11-20 : 11:08:39
|
You're very welcome. Happy Thanksgiving to you, too.JimEveryday I learn something that somebody else already knew |
|
|
|
|
|