I have a table in Oracle where I wanted to get 10% sample records after some conditions applied. I searched in Google but I am not getting correct algorithm to use SAMPLE clause after WHERE clause in Oracle.
I explained my requirement with an example below. Can you please suggest how to write sql query? My attemps are not working.
Table Name: ACCOMMODATION
Table Records:
ACCOM_ID ACCOM_TYPE
-------- ----------
1 HOTEL
2 HOTEL
3 HOTEL
4 HOTEL
5 HOUSE
6 HOUSE
7 CRUISE
Here I wanted to get 50% sample records after where condition applied. That is,
SELECT * FROM (SELECT * FROM ACCOMMODATION WHERE ACCOM_TYPE = 'HOTEL') T SAMPLE(50);
or
SELECT * FROM (SELECT * FROM ACCOMMODATION WHERE ACCOM_TYPE = 'HOUSE') T SAMPLE(50);
But above queries are not working, fails with error ORA-00933: SQL command not properly ended . If I use like below, it is not correctly returning results to me. I want only 2 random hotel records but it sometimes return 2, sometimes 3, sometimes 4.
select * from accommodation sample(50) where accom_type = 'HOTEL';
Please refer sql fiddle example HERE.
Edit: This is the sample table. Real table I use have plenty of records so unfortunately I am not able to order by dbms_random.value() as It takes very very long time to finish.