0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Quanquan Liu
  • 1,427
  • 3
  • 16
  • 30

1 Answers1

0

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

SELECT TOP 1000
    FROM OldTable as OT
    WHERE ...
    ORDER BY NEWID()
IKnowledge
  • 221
  • 3
  • 12