Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pick up multiple entries from the same column for several accounts?
    primarykey
    data
    text
    <p>I am working on extracting financial information from a couple of tables and summarizing it into another table. What I want is to select several account items from a balancesheet table by accountID, summing the items and then saving the result into another table. I need to do this for several clients. I have worked out part of the problem in this bit of code:</p> <pre><code>;with T1 AS ( SELECT CompanyID, QEndDate, Qtr, [50], [76] FROM ( SELECT CompanyID, ItemID, CAST(Amount AS DECIMAL(18,4)) AS Amount, QEndDate, Qtr from BalanceSheet WHERE CompanyID = 2335 AND (ItemID = 50 OR ItemID = 76) AND Amount &lt;&gt; '-' AND QA = 'Q' )as s PIVOT (MAX(Amount) FOR ItemID IN ([50], [76])) AS P ) UPDATE Funds SET Funds.EV = (@mCap - ([50] + [76])) / @EB FROM T1 INNER JOIN Funds ON T1.CompanyID = Funds.CompanyID </code></pre> <p>The above works fine for one Company, but I need to do several at a time.</p> <p>A little added info: The Balancesheet table contains all information as VARCHAR, hence the &lt;> '-', which some companies (but not all) use to indicate Not Applicable as opposed to zero.</p> <p>The 50 and 76 are item numbers from the Accounts table and indicate which account the amount belongs to.</p> <p>I am picking up the amounts and items from the balancesheet table and assembling them on one line so that I can then access the items, perform some math and generate a result to be stored in the Funds table. I hope that all makes sense.</p> <p>So how can I turn this into something that can perform the operations for as many customers as I need. </p> <p>Thanks, and also special thanks to all the good folks who contributed ideas and code that allowed me to get this far. </p>
    singulars
    1. This table or related slice is empty.
    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