Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting a certain number of records 2

Status
Not open for further replies.

memphisTNdev22

Programmer
Jan 3, 2006
7
US
How can I select a different number of records for different data within a column..For example...i need to select 50 records where type = 'A', 100 records where type = 'B', and 25 records where type = 'C'
 
Memphis, you can use this style of code:
Code:
SELECT <expression_list>
FROM (SELECT * FROM <source_table> WHERE type = 'A')
WHERE ROWNUM <= 50
UNION
SELECT <expression_list>
FROM (SELECT * FROM <source_table> WHERE type = 'B')
WHERE ROWNUM <= 100
UNION
SELECT <expression_list>
FROM (SELECT * FROM <source_table> WHERE type = 'C')
WHERE ROWNUM <= 25
ORDER BY <whatever order you want for the results>;
Let us know if this resolves your need.


[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
great that worked! How would I retrieve 27 percent of records = 'A', 36 percent of records = 'B', and 36 percent of records = 'C' from one table
 
Then try this code adjustment:
Code:
SELECT <expression_list>
FROM (SELECT * FROM <source_table> WHERE type = 'A')
WHERE ROWNUM <= (select .27*count(*) from <source_table> WHERE type = 'A')
UNION
SELECT <expression_list>
FROM (SELECT * FROM <source_table> WHERE type = 'B')
WHERE ROWNUM <= (select .36*count(*) from <source_table> WHERE type = 'B')
UNION
SELECT <expression_list>
FROM (SELECT * FROM <source_table> WHERE type = 'C')
WHERE ROWNUM <= (select .36*count(*) from <source_table> WHERE type = 'C')
ORDER BY <whatever order you want for the results>;


[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
SELECT <expression_list>
FROM <source_table> sample(27) WHERE type = 'A'
UNION
SELECT <expression_list>
FROM <source_table> sample(36)WHERE type = 'B'
UNION
SELECT <expression_list>
FROM <source_table> sample(36) WHERE type = 'C';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top