SELECT a random 10 records from table....easy newbie question
SELECT a random 10 records from table....easy newbie question
am 16.08.2007 12:39:49 von WhytheQ
This doesn't work:
--****************************************
USE myDatabase
SELECT TOP 10 *
FROM myTable
WHERE NEWID()
--****************************************
...I know I need to use the function newID() to find random records -
just don't know where it fits in the query (suspect the TOP will need
to go from the above)
Any help greatly appreciated
J
Re: SELECT a random 10 records from table....easy newbie question
am 16.08.2007 13:17:50 von tu_wstaw_moje_imie
WhytheQ pisze:
> This doesn't work:
>
> --****************************************
> USE myDatabase
> SELECT TOP 10 *
> FROM myTable
> WHERE NEWID()
> --****************************************
>
> ..I know I need to use the function newID() to find random records -
> just don't know where it fits in the query (suspect the TOP will need
> to go from the above)
>
> Any help greatly appreciated
Replace "WHERE" with "ORDER BY":
SELECT TOP 10 *
FROM myTable
ORDER BY NEWID()
IMHO this query won't be efficient against large tables.
--
Best regards,
Marcin Guzowski
http://guzowski.info
Re: SELECT a random 10 records from table....easy newbie question
am 16.08.2007 22:23:38 von Hugo Kornelis
On Thu, 16 Aug 2007 03:39:49 -0700, WhytheQ wrote:
>This doesn't work:
>
>--****************************************
>USE myDatabase
>SELECT TOP 10 *
>FROM myTable
>WHERE NEWID()
>--****************************************
>
>..I know I need to use the function newID() to find random records -
>just don't know where it fits in the query (suspect the TOP will need
>to go from the above)
>
>Any help greatly appreciated
>J
Hi J,
Marcin's answer is correct. However, if you're on SQL Server 2005 you
can also use the new TABLESAMPLE option (see Books Online for the
details); this will probably perform better on large tables.
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Re: SELECT a random 10 records from table....easy newbie question
am 17.08.2007 08:12:21 von soren
Hugo Kornelis wrote:
> On Thu, 16 Aug 2007 03:39:49 -0700, WhytheQ wrote:
>=20
>> This doesn't work:
>>
>> --****************************************
>> USE myDatabase
>> SELECT TOP 10 *
>>FROM myTable
>> WHERE NEWID()
>> --****************************************
>>
>> ..I know I need to use the function newID() to find random records -
>> just don't know where it fits in the query (suspect the TOP will need
>> to go from the above)
>>
>> Any help greatly appreciated
>> J
>=20
> Hi J,
>=20
> Marcin's answer is correct. However, if you're on SQL Server 2005 you
> can also use the new TABLESAMPLE option (see Books Online for the
> details); this will probably perform better on large tables.
Do you have a link to the book you are talking about ?
p.s. You blog is to large for pageflakes :( (max 512000 bytes) could you =
maybe cut down the articles in the feed ?
Best regards
S=F8ren
Re: SELECT a random 10 records from table....easy newbie question
am 17.08.2007 12:56:10 von WhytheQ
This comes up with nothing:
'=====================
USE myDatabase
SELECT *
FROM myTable
TABLESAMPLE (1 ROWS)
'====================
....whereas this works:
'=====================
USE myDatabase
SELECT *
FROM myTable
TABLESAMPLE (1 PERCENT)
'====================
Any ideas??
J
On 16 Aug, 21:23, Hugo Kornelis
wrote:
> On Thu, 16 Aug 2007 03:39:49 -0700,WhytheQwrote:
> >This doesn't work:
>
> >--****************************************
> >USE myDatabase
> >SELECT TOP 10 *
> >FROM myTable
> >WHERE NEWID()
> >--****************************************
>
> >..I know I need to use the function newID() to find random records -
> >just don't know where it fits in the query (suspect the TOP will need
> >to go from the above)
>
> >Any help greatly appreciated
> >J
>
> Hi J,
>
> Marcin's answer is correct. However, if you're on SQL Server 2005 you
> can also use the new TABLESAMPLE option (see Books Online for the
> details); this will probably perform better on large tables.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis- Hide quoted text -
>
> - Show quoted text -
Re: SELECT a random 10 records from table....easy newbie question
am 18.08.2007 00:13:55 von Erland Sommarskog
Søren Reinke (soren@REMOVE.reinke.dk) writes:
> Hugo Kornelis wrote:
>> Marcin's answer is correct. However, if you're on SQL Server 2005 you
>> can also use the new TABLESAMPLE option (see Books Online for the
>> details); this will probably perform better on large tables.
See my signature. Or your hard disk (if you have SQL Server installed).
> p.s. You blog is to large for pageflakes :( (max 512000 bytes) could you
> maybe cut down the articles in the feed ?
Pageflakes?
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Re: SELECT a random 10 records from table....easy newbie question
am 18.08.2007 00:39:45 von Hugo Kornelis
On Fri, 17 Aug 2007 08:12:21 +0200, Søren Reinke wrote:
>Do you have a link to the book you are talking about ?
Hi Søren,
Erland already adressed this question.
>p.s. You blog is to large for pageflakes :( (max 512000 bytes) could you
>maybe cut down the articles in the feed ?
I'll forward this concern to the administrators of the blog. In the
meantime, may I suggest that you read it online?
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Re: SELECT a random 10 records from table....easy newbie question
am 18.08.2007 00:44:55 von Hugo Kornelis
On Fri, 17 Aug 2007 10:56:10 -0000, WhytheQ wrote:
>This comes up with nothing:
>'=====================
>USE myDatabase
>SELECT *
>FROM myTable
>TABLESAMPLE (1 ROWS)
>'====================
>
>...whereas this works:
>'=====================
>USE myDatabase
>SELECT *
>FROM myTable
>TABLESAMPLE (1 PERCENT)
>'====================
>
>Any ideas??
Hi J,
TABLESAMPLE won't give you the exact number of rows you asked for, but
an approximation. This is explained in Books Online (though not as
clearly as I hoped and thought it was).
Also see Erland's reply in your other thread.
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Re: SELECT a random 10 records from table....easy newbie question
am 18.08.2007 00:50:38 von soren
Erland Sommarskog wrote:
> S=F8ren Reinke (soren@REMOVE.reinke.dk) writes:
>> Hugo Kornelis wrote:
>>> Marcin's answer is correct. However, if you're on SQL Server 2005 you=
>>> can also use the new TABLESAMPLE option (see Books Online for the
>>> details); this will probably perform better on large tables.
>=20
> See my signature. Or your hard disk (if you have SQL Server installed).=
Lol thanks :)
Never thought about looking there, found them :)
> =20
>> p.s. You blog is to large for pageflakes :( (max 512000 bytes) could y=
ou=20
>> maybe cut down the articles in the feed ?
>=20
> Pageflakes?
>=20
http://www.pageflakes.com/
A great way to handle a lot of RSS feeds, and it does not keep=20
forgetting like Google's does, and is faster than the one from microsoft.=
/S=F8ren
Re: SELECT a random 10 records from table....easy newbie question
am 18.08.2007 01:01:06 von Hugo Kornelis
On Fri, 17 Aug 2007 08:12:21 +0200, Søren Reinke wrote:
>p.s. You blog is to large for pageflakes :( (max 512000 bytes) could you
>maybe cut down the articles in the feed ?
Hi Søren.
Here's the response of Peter DeBetta:
"I just added the main feed to pageflakes without issue. Could you
have this fellow write to me directly so I can work with him on
resolving his issue."
You can use the "Click here to send email" link on the blogs front page
to contact Peter.
Thanks for your interest in sqlblog.com!
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Re: SELECT a random 10 records from table....easy newbie question
am 18.08.2007 15:09:41 von soren
Hugo Kornelis wrote:
> On Fri, 17 Aug 2007 08:12:21 +0200, S=F8ren Reinke wrote:
>=20
>> p.s. You blog is to large for pageflakes :( (max 512000 bytes) could y=
ou=20
>> maybe cut down the articles in the feed ?
>=20
> Hi S=F8ren.
>=20
> Here's the response of Peter DeBetta:
>=20
> "I just added the main feed to pageflakes without issue. Could you
> have this fellow write to me directly so I can work with him on
> resolving his issue."
>=20
> You can use the "Click here to send email" link on the blogs front page=
> to contact Peter.
>=20
> Thanks for your interest in sqlblog.com!
>=20
Sure i will contact them directly.
See ya
/S=F8ren