简体   繁体   中英

SELECT DISTINCT random columns limited by a number

I have a very specific question regarding using SELECT DISTINCT...INTO...

I want to select exactly 1000 random rows into a new table using SELECT DISTINCT which forms combinations of rows from my previous table. For example, my current query is:

SELECT DISTINCT p1.id AS id1, p2.id AS id2 INTO my_new_table FROM oldtable AS p1, oldtable AS p2;

How do I modify this so that I am selecting at most 1000 random rows? I'm dealing with millions of records so a solution that is efficient is obviously desired.

This is how I would go about selecting random 1000 rows quickly:

SELECT TOP 1000
    FROM OldTable as OT
    WHERE ...
    ORDER BY NEWID()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM