October 13, 2002 at 11:51 pm
If I had a table with say a list of 10 names.
col_id mfg_name
1 ACER
2 COMPAQ
3 AMD
4 INTEL
and so on and I ahd a col_id for each how could I create a query that would grab a random col_id ??
For example.
select * from table where col_id=Random Number
Thnaks,
Andrew
Andrew
http://eshopsoho.com
October 14, 2002 at 12:43 am
create table test1 (filed1 int identity, field2 int)
insert into test1(field2) values(1002);
insert into test1(field2) values(1003);
select * from test1
Hope this helps.
Thanks and regards,
Anbu
October 14, 2002 at 12:52 am
Anbu,
Thanks, I figured this one out using cold fusion and RANDRANGE.
Thanks,
Andrew
Andrew
http://eshopsoho.com
October 14, 2002 at 1:30 pm
declare @i int
select @i = count(*) * rand() + 1 from mytable
select @i
select top 1 *
from myTable mt1
where @i = (select count(*) from myTable mt2 where mt2.col_id <= mt1.col_id)
This allows for non sequential numbers in col_id
Simon Sabin
Co-author of SQL Server 2000 XML Distilled
http://www.amazon.co.uk/exec/obidos/ASIN/1904347088
Simon Sabin
SQL Server MVP
http://sqlblogcasts.com/blogs/simons
October 15, 2002 at 1:25 am
A different approach using NEWID() is shown here (http://www.sql.nu/read.asp?id=4), and it works nice if you have a small table (like with 10 rows).
--
Chris Hedgate @ Apptus Technologies (http://www.apptus.se)
October 16, 2002 at 7:06 am
Hi
You can try
SELECT TOP 1 NEWID(), * FROM yourTble ORDER BY 1
hth
el.c.
myLittleTools.net :: web-based applications
myLittleTools.net :: web-based applications
http://www.mylittletools.net
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply