Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would do this in SQL personally. You can use a SUM() aggregate to get the value you're after for each account. You won't want to sum the percents though, you'll want to SUM the values and then create the percent afterwards once you have the totals. A sum of averages isn't the same as an average of sums (except in some circumstances but we can't assume that).</p> <p>I would do something like this, though you'll probably need to tweak it to get it working correctly for you. (I also may not fully understanding the meaning of all the columns you're using).</p> <pre><code>SELECT PortfolioBaseCode,totalValue,cashValue, (CAST(cashValue AS FLOAT)/totalValue) * 100 AS pcntCash FROM ( SELECT b.PortfolioBaseCode, SUM(a.MarketValue) AS totalValue SUM(CASE s.SecuritySymbol WHEN 'CASH' THEN a.MarketValue ELSE 0 END) AS cashValue FROM APXUser.fAppraisal (@ReportData) a LEFT JOIN APXUser.vPortfolioBaseSettingEx b ON b.PortfolioBaseID = a.PortfolioBaseID LEFT JOIN APXUser.vSecurityVariant s ON s.SecurityID = a.SecurityID AND s.SecTypeCode = a.SecTypeCode AND s.IsShort = a.IsShortPosition AND a.PercentAssets &gt;= @PercentAssets GROUP BY b.PortfolioBaseCode ) AS t WHERE (CAST(cashValue AS FLOAT)/totalValue)&gt;=@pcntParameter </code></pre> <p>Alternatively you can use the HAVING clause to filter aggregate functions like a WHERE clause would (though I find this a little less readable):</p> <pre><code>SELECT b.PortfolioBaseCode, SUM(a.MarketValue) AS totalValue SUM(CASE s.SecuritySymbol WHEN 'CASH' THEN a.MarketValue ELSE 0 END) AS cashValue, (SUM(a.MarketValue)/SUM(CASE s.SecuritySymbol WHEN 'CASH' THEN a.MarketValue ELSE 0 END))*100 AS cashPcnt FROM APXUser.fAppraisal (@ReportData) a LEFT JOIN APXUser.vPortfolioBaseSettingEx b ON b.PortfolioBaseID = a.PortfolioBaseID LEFT JOIN APXUser.vSecurityVariant s ON s.SecurityID = a.SecurityID AND s.SecTypeCode = a.SecTypeCode AND s.IsShort = a.IsShortPosition AND a.PercentAssets &gt;= @PercentAssets GROUP BY b.PortfolioBaseCode HAVING (SUM(a.MarketValue)/SUM(CASE s.SecuritySymbol WHEN 'CASH' THEN a.MarketValue ELSE 0 END))&gt;=@pcntParameter </code></pre> <p>Basically you can use grouping and aggregate functions to get the total value and the cash value for each account and then calculate the proper percentage from that. This will take into account any negatives, etc in other holdings.</p> <p>Some references for aggregate function and grouping:</p> <p><a href="http://technet.microsoft.com/en-us/library/ms177673.aspx" rel="nofollow">MSDN - GROUP BY (TSQL)</a></p> <p><a href="http://technet.microsoft.com/en-us/library/ms173454.aspx" rel="nofollow">MSDN - Aggregate Functions</a></p> <p><a href="http://technet.microsoft.com/en-us/library/ms180199.aspx" rel="nofollow">MSDN - HAVING Clause</a></p> <p>If you find you're needing to do some kind of aggregation but you cannot group your results you should look into using the TSQL window functions which are very handy:</p> <p><a href="https://www.simple-talk.com/sql/learn-sql-server/working-with-window-functions-in-sql-server/" rel="nofollow">Working with Window Functions in SQL Server</a></p> <p><a href="http://technet.microsoft.com/en-us/library/ms189461.aspx" rel="nofollow">MSDN - OVER Clause</a></p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      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