How to select number of record based on a count in a subquery

Viewed 337

I need to return the X number of records in a table based on the count of records in a subquery.

For example, if the TOP 80 PERCENT of records in MYTABLE equals 275 records, then I want to select 275 records from another table.

Can this be done with just plain dynamic SQL, and without creating variables etc?

My predecessor wrote something like this:

DECLARE @RecordVariable int

SET @RecordVariable =
(SELECT COUNT(*) * .8 
FROM MYTABLE)

SELECT TOP (@RecordVariable) *
FROM
MYOTHERTABLE
ORDER BY NEWID()
2 Answers
Related