Question

I was wondering if there was a way to change truncate into round when doing a top x percent.

For Example:

Select Top 10 Percent
    HospMastID
        ,ClientID
    ,ControlGroup = 1
    Into
    #RandomTable
    From
    #ClientsTable
    Order By
    NewID()

At present when I have a total of 1176 original records it returns 117 as the top 10 percent. Just curious what the setting would be to change this. Since it is really truncating the original numbers instead of rounding it.

Thanks, Scott

Was it helpful?

Solution

If you want to ROUND the results, you can calculate your own value to use in TOP (granted, this means that you need to first count the rows of your table, but it's the only way that I can think of doing this, since there isn't a setting for this):

DECLARE @TopPercent INT, @Top INT
SET @TopPercent = 10 -- use the value you want here

SELECT @Top = ROUND(COUNT(*)*CAST(@TopPercent AS DECIMAL(4,1))/100,0)
FROM #ClientsTable

SELECT TOP(@Top) 
        HospMastID,
        ClientID,
        ControlGroup = 1
INTO #RandomTable
FROM #ClientsTable
ORDER BY NEWID()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top