Select a random sample of results from a query result

后端 未结 8 799
-上瘾入骨i
-上瘾入骨i 2020-11-30 20:14

This question asks about getting a random(ish) sample of records on SQL Server and the answer was to use TABLESAMPLE. Is there an equivalent in Oracle 10?

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 20:23

    The SAMPLE clause will give you a random sample percentage of all rows in a table.

    For example, here we obtain 25% of the rows:

    SELECT * FROM emp SAMPLE(25)
    

    The following SQL (using one of the analytical functions) will give you a random sample of a specific number of each occurrence of a particular value (similar to a GROUP BY) in a table.

    Here we sample 10 of each:

    SELECT * FROM (
    SELECT job, sal, ROW_NUMBER()
    OVER (
    PARTITION BY job ORDER BY job
    ) SampleCount FROM emp
    )
    WHERE SampleCount <= 10
    

提交回复
热议问题