John said:
As far as I know, the Top predicate rounds UP to the next whole number when
using PerCent.
I know of no easy way do what you seem to want - round the calculated
percent down or round it to the nearest whole number.
Considering TOP 5 could be re-written using standard SQL as this:
SELECT T1.data_col
FROM Test6 AS T1
WHERE 5 >= (
SELECT COUNT(*)
FROM Test6 AS T2
WHERE T2.data_col <= T1.data_col
);
then I think a TOP 10% with rounding to the nearest could be this:
SELECT T1.data_col
FROM Test6 AS T1
WHERE (
SELECT COUNT(*) * 10/100
FROM Test6 AS T3) >= (
SELECT COUNT(*)
FROM Test6 AS T2
WHERE T2.data_col <= T1.data_col
);
The idea of using PARAMETERS is a good one and something that can't be
achieved with TOP N. Here's a quick demo:
Sub TopPercent()
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\DropMe.mdb"
With .ActiveConnection
' Create test table
.Execute _
"CREATE TABLE Test6 (" & _
" data_col INTEGER NOT NULL)"
' Create test data (incrementing integers 1-12)
.Execute _
"INSERT INTO Test6 (data_col) VALUES (1);"
.Execute _
"INSERT INTO Test6 (data_col) SELECT DT1.data_col" & _
" FROM ( SELECT 2 AS data_col FROM Test6" & _
" UNION ALL SELECT 3 FROM Test6 UNION ALL" & _
" SELECT 4 FROM Test6 UNION ALL SELECT 5" & _
" FROM Test6 UNION ALL SELECT 6 FROM Test6" & _
" UNION ALL SELECT 7 FROM Test6 UNION ALL" & _
" SELECT 8 FROM Test6 UNION ALL SELECT 9" & _
" FROM Test6 UNION ALL SELECT 10 FROM Test6" & _
" UNION ALL SELECT 11 FROM Test6 UNION ALL" & _
" SELECT 12 FROM Test6 ) AS DT1;"
' Create procedure
.Execute _
"CREATE PROCEDURE ProcTest6 (arg_percent" & _
" DECIMAL(3, 2) = 1.00) AS SELECT T1.data_col" & _
" FROM Test6 AS T1 WHERE (SELECT COUNT(*)" & _
" * arg_percent FROM Test6 AS T3) >= (SELECT" & _
" COUNT(*) FROM Test6 AS T2 WHERE T2.data_col" & _
" <= T1.data_col);"
' Show results for 'top 10%'
Dim rs
Set rs = .Execute("EXECUTE ProcTest6 0.1")
MsgBox rs.GetString
rs.Close
End With
Set .ActiveConnection = Nothing
End With
End Sub
Jamie.
--