display different sort order for each execution

  • Hello all,

    i would like to know if there is a way to display results in random order for each execution.

    /*-------------------------Drop temp table-------------------------------------------*/

    IF OBJECT_ID('tempdb..#temp') IS NOT NULL

    DROP TABLE #temp;

    create table #temp (col1 int, col2 varchar(4))

    insert into #temp

    select 7, 'AB'

    UNION SELECT 2, 'CD'

    UNION SELECT 9, 'JQ'

    UNION SELECT 12, '3A'

    UNION SELECT 3, '8D'

    UNION SELECT 1, 'ER'

    SELECT * FROM #TEMP

    i want to display results in different sort order each time is it possible?

  • Nothing to it

    SELECT * FROM #TEMP

    ORDER BY NEWID();

    NEWID() generates a random number for each record, so sorting by it causes a "random" sort.

  • mxy (7/18/2016)


    Hello all,

    i would like to know if there is a way to display results in random order for each execution.

    Out of curiosity, why do you want to do that?

    😎

    As Pieter pointed out, this is very simple to do but can be quite hefty work for the server if the result set is large.

  • i have store data and requirement for monthly promotions is show promotions in a random order

    not leaning towards one brand 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply