Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't need to actually use the row number for anything, then I would just go with getting the TOP 1 row. The query could be simplified a lot. I like to start by first selecting from the table that will be filtered out the most by your predicates. Hopefully, you have a good index on frs.fundraiserid and all of the columns participating in the joins.</p> <pre><code>SELECT TOP 2 customercampaignname + ' $' + CONVERT(VARCHAR(255), CAST(SUM(d.mastercurrencyamount) AS NUMERIC(36,2) ) ) AS 'Check_Stub_Comment2', ROW_NUMBER() OVER(ORDER BY SUM(d.mastercurrencyamount) DESC) as rownumber FROM bb02_fundraiserrevenuestream frs JOIN bb02_donationline dl ON dl.fundraiserrevenuestreamid = frs.fundraiserrevenuestreamid JOIN bb02_donation d ON d.donationid = dl.donationid WHERE frs.fundraiserid = 1869 AND d.customercampaignname IS NOT NULL AND d.customercampaignname != '' GROUP BY d.CustomerCampaignName ORDER BY SUM(d.mastercurrencyamount) DESC </code></pre> <p>Since you need to be able to select either the 1st or 2nd row, then wrap it up in a CTE or subquery.</p> <pre><code>WITH topcampaign AS ( SELECT TOP 2 customercampaignname + ' $' + CONVERT(varchar(255), CAST(SUM(d.mastercurrencyamount) AS NUMERIC(36,2) ) ) AS 'Check_Stub_Comment2', ROW_NUMBER() OVER(ORDER BY SUM(d.mastercurrencyamount) DESC) as rownumber FROM bb02_fundraiserrevenuestream frs JOIN bb02_donationline dl ON dl.fundraiserrevenuestreamid = frs.fundraiserrevenuestreamid JOIN bb02_donation d ON d.donationid = dl.donationid WHERE frs.fundraiserid = 1869 AND d.customercampaignname IS NOT NULL AND d.customercampaignname != '' GROUP BY d.CustomerCampaignName ORDER BY SUM(d.mastercurrencyamount) DESC ) SELECT * from topcampaign WHERE rownumber = 1 </code></pre> <p>As another possible optimization, I took the CONVERT out of the CTE and put it in the final select. Not sure if that helps much.</p> <pre><code>WITH topcampaign AS ( SELECT TOP 2 customercampaignname, SUM(d.mastercurrencyamount) AS amount, ROW_NUMBER() OVER(ORDER BY SUM(d.mastercurrencyamount) DESC) as rownumber FROM bb02_fundraiserrevenuestream frs JOIN bb02_donationline dl ON dl.fundraiserrevenuestreamid = frs.fundraiserrevenuestreamid JOIN bb02_donation d ON d.donationid = dl.donationid WHERE frs.fundraiserid = 1869 AND d.customercampaignname IS NOT NULL AND d.customercampaignname != '' GROUP BY d.CustomerCampaignName ORDER BY SUM(d.mastercurrencyamount) DESC ) SELECT rownumber, customercampaignname + ' $' + CONVERT(varchar(255), CAST(amount AS NUMERIC(36,2) ) ) AS 'Check_Stub_Comment2' FROM topcampaign WHERE rownumber = 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload