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 |
|
dmaxj
Posting Yak Master
174 Posts |
Posted - 2011-08-31 : 13:20:57
|
Hello - My table data exampleCode Description--------------------------1111 Television421111 Television441111 Television602222 Radio1002222 Radio2003333 Stove993333 Stove45 How can get the following output -Code Description--------------------------1111 Television422222 Radio1003333 Stove99 Regards |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-08-31 : 13:23:23
|
| You can't.Either you will have some sort of additional Sequence column (date created, IDENTITY, or something else), that you can use to create an ordering effect.Or you will have to use MIN(Description) to get the lowest value.Or you can just take one description, at random, for each distinct Code value. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-31 : 13:24:32
|
| [code]select Code,descriptionfrom(select Code,Description,ROW_NUMBER() OVER (PARTITION BY Code ORDER BY Description) AS SeqFROM table)twhere Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-08-31 : 13:34:33
|
| "PARTITION BY ... ORDER BY Description"Unfortunately that will give you Stove45 for Code3333, rather than Stove99 as per the requirement (which is the physically first row, rather than the order-ably first row).@dmaxj : RDBMS Database has no concept of "the order the records were created" - unless you have a column with CreateDate or ID number or somesuch to ORDER BY |
 |
|
|
dmaxj
Posting Yak Master
174 Posts |
Posted - 2011-08-31 : 14:30:27
|
| I am just looking for any Description with a single Code, so I think that this code will suffice - Thanks to you all! |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-08-31 : 16:56:10
|
"I am just looking for any Description with a single Code"In that case I think:SELECT Code, [Description] = MIN(Description)FROM MyTableGROUP BY CodeORDER BY Code will be more efficient |
 |
|
|
|
|
|
|
|