Note that there are some explanatory texts on larger screens.

plurals
  1. POneed help optimized sql query
    primarykey
    data
    text
    <p>I have a web site that tracks mutual fund investment. for this I have below table structure (structure has been modified to remove unwanted columns so as to bring focus on my problem)</p> <p><strong>Table #1: scheme_Mst</strong></p> <pre><code>|Scheme_ID | fundHouse_Id | OpeningBalance | OpeningUnits ---------------------------------------------------------- |1 | 1 | 100 | 10 </code></pre> <p><strong>Table #2 investment</strong></p> <pre><code>|Scheme_ID | InvestedAmt | InvestedUnits | statusFlag ---------------------------------------------------------- |1 | 50 | 5 | A </code></pre> <p><strong>Table #3 redemption</strong></p> <pre><code>|Scheme_ID | redemedAmt | redemedUnits | statusFlag ---------------------------------------------------------- |1 | 50 | 5 | A </code></pre> <p><strong>Table #4 SwitchDetails</strong></p> <pre><code>|From_Scheme_ID |To_Scheme_ID |switchInAmt |switchInUnits |switchOutAmt |switchOutUnits | StaFlag ------------------------------------------------------------------------------------------ |1 | 2 | 20 | 2 | 10 | 1 | A </code></pre> <p><strong>Table #5 BonusDetails</strong></p> <pre><code>|Scheme_ID | BonusAmt | BonusUnits | statusFlag ---------------------------------------------------------- |1 | 50 | 5 | A </code></pre> <p><strong>Table #6 Divident_Detais</strong></p> <pre><code>|Scheme_ID | DividentAmt | DividentUnits | statusFlag ---------------------------------------------------------- |1 | 50 | 5 | A </code></pre> <p>Now to have a details of the of available units in the scheme I have below query ....(Which is very very expensive )</p> <pre><code>SELECT *, CASE WHEN OutstandingUnits &lt;&gt; 0 THEN CONVERT(decimal(18,2),Outstanding/OutstandingUnits) ELSE 0 END AS WAC FROM (SELECT *, ISNULL(OpeningBalance,0) + ISNULL(DividendAmount,0) + ISNULL(bonusAmount,0) + ISNULL(invstAmount,0) + ISNULL(SwitchedInAmount,0) - ISNULL(redeemedAmount,0) - ISNULL(SwitchedOutAmount,0) AS Outstanding, ISNULL(openingUnits,0) + ISNULL(DividendUnits,0) + ISNULL(bonusUnits,0) + ISNULL(invstUnits,0) + SNULL(SwitchedInUnits,0) - ISNULL(redeemedUnits,0) - ISNULL(SwitchedOutUnits,0) AS OutstandingUnits FROM (SELECT C.scheme_ID,D.schemeName, D.Openingbalance AS OpeningBalance, D.OpeningUnits AS openingUnits, ISNULL(SUM(C.invstAmount),0) AS invstAmount, ISNULL(SUM(C.invstUnits),0) AS invstUnits, (SELECT ISNULL(SUM(redemedAmt),0) FROM redemption WHERE StatusFlag='A' AND scheme_ID = C.scheme_ID) AS redeemedAmount, (SELECT ISNULL(SUM(redeemedUnits),0) FROM redemption WHERE StatusFlag='A' AND scheme_ID = C.scheme_ID) AS redeemedUnits, (SELECT ISNULL(sum(switchOutAmt),0) FROM SwitchDetails WHERE BB.Status = 'A' AND BB.From_Scheme_Id = C.scheme_ID) AS SwitchedOutAmount, (SELECT ISNULL(sum(switchOutUnits),0) FROM SwitchDetails WHERE BB.Status = 'A' AND BB.From_Scheme_Id = C.scheme_ID) AS SwitchedOutUnits, (SELECT ISNULL(sum(switchOutAmt),0) FROM SwitchDetails WHERE BB.Status = 'A' AND BB.From_Scheme_Id = C.scheme_ID) AS SwitchedInAmount, (SELECT ISNULL(sum(SwitchedInUnits),0) FROM SwitchDetails WHERE BB.Status = 'A' AND BB.From_Scheme_Id = C.scheme_ID) AS SwitchedInUnits, .. same way FOR bonus AND divident .. FROM Investment c) tab)tab2 </code></pre> <p>This query is supposed to produce the below output.. (sample output)</p> <pre><code>Scheme_ID| Scheme_Name | OpeningBalance | OpeningUnits | InvstAmount | invstUnits | redemedAmount | redemedUnits | SwitchedOutAmt | SwitchOutUnit | bonusAmt | bonusUnit | DividentAmount | DividentUnit | Outstanding | OutstandingUnit | WAC ------------------------------------------------------------------------------------------- </code></pre> <p>This is how I am calculating the details. Please suggest me the better way of doing it.</p> <p>I am using sql server 2008 </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. 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