Home Forums Programming General get set of random rows with distinct values RE: get set of random rows with distinct values

  • by introducing a row_number function that partitions by the two columns, you cna find unique rows; fromt here, it's just the same query you had before, but hitting a subquery isntead of the raw table:

    SELECT

    TOP 6

    WORKID,

    AUTHORID

    FROM (SELECT

    ROW_NUMBER() OVER(PARTITION BY WORKID, AUTHORID ORDER BY WORKID, AUTHORID) AS RW,

    WORKID,

    AUTHORID

    FROM dbo.LITERARYWORKS) MyAlias

    WHERE RW = 1--finds Uniqueness across WORKID, AUTHORID

    ORDER BY

    CHECKSUM(NEWID())

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!