The sum of all SQL Server matter that exist,
and the space in which all SQL Server events
occur or could occur.
Welcome to SQL Server Universe.com Sign in | Join | Help
in
Home SS SLUG Forums Articles Photos Downloads

random selecting

Last post 09-11-2008, 14:53 by Gogula G. Aryalingam. 1 replies.
Sort Posts: Previous
  • random selecting

     09-11-2008, 14:15

    • Joined on 08-10-2008
    • Posts 9
    • Points 0
    • Top 25 Contributor

    hi,

     want to know  how can i randomly select a tuple in a table and get its colounms' data?

     

  • Re: random selecting

     09-11-2008, 14:53

    • Joined on 11-22-2006
    • Colombo, Sri Lanka
    • Posts 230
    • Points 0
    • Top 10 Contributor
      Male

    Hi,

    This could be done in several ways. Here's a script that I came up with. It uses the Products table of the AdventureWorks database. It randomly selects a record and displays the [Name] and [ProductNumber] columns:

    SELECT
        [Name],
        [ProductNumber]
    FROM
        (
            SELECT
                ROW_NUMBER() OVER (ORDER BY [ProductID]) AS Counter,
                [Name],
                [ProductNumber]
            FROM
                Production.Product
        ) Prod
    WHERE
        Prod.Counter = ROUND(RAND() * (SELECT COUNT(*) FROM Production.Product),0)

     

    Any other ideas?

    Cheers


    Gogula G. Aryalingam (MVP - SQL Server)

    DB Antics

View as RSS news feed in XML

(Best viewed with a resolution of more than 1024 * 768)

Powered by Community Server, by Telligent Systems