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 |
pushp82
Yak Posting Veteran
83 Posts |
Posted - 2013-08-22 : 01:56:00
|
I have a table ID CODE NAME611 GI08 Gian610 GI07 Gian618 GI15 Gian620 FT Rope320 ST Soler345 ST01 SolerI need the out put one random value from same namelike ID CODE NAME611 GI08 Gian (it should be random)620 FT Rope320 ST Soler (it should be random)Please help! |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-08-22 : 02:59:31
|
[code]SELECT ID , CODE , NAME FROM (SELECT *, RANK() OVER(PARTITION BY NAME ORDER BY NEWID() ) RN FROM TableName ) TWHERE RN=1[/code]--Chandu |
 |
|
erikhaselhofer
Starting Member
30 Posts |
Posted - 2013-08-22 : 03:00:54
|
Someone may have a smarter way of doing this but it worked on the test data,create table junk(id int,description char(20),type char(20));INSERT INTO junk VALUES (611,'GI08','Gian');INSERT INTO junk VALUES (610,'GI07','Gian');INSERT INTO junk VALUES (618,'GI15','Gian');INSERT INTO junk VALUES (620,'FT','Rope');INSERT INTO junk VALUES (320,'ST','Soler');INSERT INTO junk VALUES (345,'ST01','Soler');SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY type ORDER BY newid()) AS rowNumber, *FROM junk) c2WHERE rowNumber = 1rowNumber id description type1 611 GI08 Gian 1 620 FT Rope 1 345 ST01 Soler |
 |
|
erikhaselhofer
Starting Member
30 Posts |
Posted - 2013-08-22 : 03:02:12
|
quote: Originally posted by erikhaselhofer Bah, gonna leave it anyway!!!!create table junk(id int,description char(20),type char(20));INSERT INTO junk VALUES (611,'GI08','Gian');INSERT INTO junk VALUES (610,'GI07','Gian');INSERT INTO junk VALUES (618,'GI15','Gian');INSERT INTO junk VALUES (620,'FT','Rope');INSERT INTO junk VALUES (320,'ST','Soler');INSERT INTO junk VALUES (345,'ST01','Soler');SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY type ORDER BY newid()) AS rowNumber, *FROM junk) c2WHERE rowNumber = 1rowNumber id description type1 611 GI08 Gian 1 620 FT Rope 1 345 ST01 Soler
|
 |
|
erikhaselhofer
Starting Member
30 Posts |
Posted - 2013-08-22 : 03:03:10
|
Oops, doubled up. |
 |
|
pushp82
Yak Posting Veteran
83 Posts |
Posted - 2013-08-22 : 05:52:06
|
thanks bandi and erikhaselhofer, I was just moving around the query but you made my day. |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-08-22 : 07:18:13
|
quote: Originally posted by pushp82 thanks bandi and erikhaselhofer, I was just moving around the query but you made my day.
Welcome --Chandu |
 |
|
erikhaselhofer
Starting Member
30 Posts |
Posted - 2013-08-22 : 12:46:51
|
quote: Originally posted by pushp82 thanks bandi and erikhaselhofer, I was just moving around the query but you made my day.
Happy to help and I learned something too: that isn't an edit button. |
 |
|
|
|
|
|
|