From: LightBulb on 3 Feb 2010 15:25 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 3 Feb 2010 16:35 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
|
Pages: 1 Prev: Backups without data? Next: Installation Problem SQL Server 2005 + Windows 7 |