From: LightBulb on
Hi...I need to return the Top 20 customers by 'Accounts Receivable
Balance' (= FutureBal + Per1Bal + ... + Per5Bal + Misc_Creds +
Unapplied) in the and then in the select list sum a calculated value,
Sum(Val1 -Val), and another. I am stuck on how to use TOP in a WHERE
Clause.. TIA LB
From: Plamen Ratchev on
Here are two solutions:

SELECT SUM(<column>) AS total
FROM (
SELECT TOP 20 <columns>
FROM Table
ORDER BY <expression>) AS T;

SELECT SUM(<column>) AS total
FROM (
SELECT <columns>,
ROW_NUMBER() OVER(ORDER BY <expression>) AS rk
FROM Table) AS T
WHERE rk <= 20;

--
Plamen Ratchev
http://www.SQLStudio.com