Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4330498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:58:16+00:00 2026-05-21T09:58:16+00:00

I have a table containing a list of trades: Security ; Quantity ; Price

  • 0

I have a table containing a list of trades:

Security ;   Quantity ; Price ;  Consid
 1. IBM  ;    +1,000 ;   20  ;  -20k
 2. IBM  ;    +2,000 ;   22  ;  -44k
 3. IBM  ;    -1,000 ;   30  ;  +30k
 4. IBM  ;    -2,000 ;   20  ;  +40k
 5. IBM  ;    -2,000 ;   20  ;  -20k

So the PnL is basically the sum of the Consid column so before Trade#5 was added the PnL would be +6k.

After Trade #5 was added this shows the PnL as -14k which isnt really that reflective of where we stand.

What I would like is some way of filtering out the not closed trades? So Trade#5 will only be allowed into the sum when we have added a purchase of 2k IBM shares to the table.

My intial attempt at this was:

set @Ret = @Ret + isnull((SELECT SUM(GC) AS GS
FROM  (SELECT SUM(GrossConsid) * - 1 AS GC
                     FROM   Trades AS CT
                     WHERE  (SpecialCond = 'Prop') AND (SettType <> 'Futures') AND (TrdDt <= @Date) AND (TrdDt >=@StartDate) AND (Name = 'my_Comp')
                     GROUP BY ABS(Quantity)
                     HAVING (SUM(Quantity) = 0)) AS dt),0)

but I have no figured out that there is an edge condition where by if I have a trades with a Quantity of +5,+5,-5 it doesnt get counted because the (SUM(Quantity) = 0)) evaluates to false.

Any ideas on how I can rectify this?

Thanks Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:58:16+00:00Added an answer on May 21, 2026 at 9:58 am

    Runnable Example

    DECLARE @tbl AS TABLE (Seq int, Security varchar(3), Quantity int, Price int, Consid int) ;
    INSERT INTO @tbl VALUES
    (1, 'IBM', 1000, 20, -20000)
    ,(2, 'IBM', 2000, 22, -44000)
    ,(3, 'IBM', -1000, 30, 30000)
    ,(4, 'IBM', -2000, 20, 40000)
    ,(5, 'IBM', -2000, 20, -20000);
    
    WITH RunningInventory AS (
    SELECT l.Seq, SUM(r.Quantity) AS Inv
    FROM @tbl AS l
    LEFT JOIN @tbl r
    ON r.Seq <= l.Seq
    GROUP BY l.Seq
    )
    SELECT *
    FROM @tbl AS trx
    INNER JOIN RunningInventory
        ON trx.Seq = RunningInventory.Seq
    WHERE RunningInventory.Inv >= 0 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a code table containing a list of types (Type_ID, Description), but
I have an R list containing a series of tables. Each table in the
I have a table lists containing list information like title and id (auto_increment) and
I have a table containing a list of folders, each folder has an ID
I have a table containing a list of individual prices for a variety of
I have two tables containing Tasks and Notes, and want to retrieve a list
I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I
I have a table containing hundreds of entries and I am trying to delete
I have a table containing cells with text of various lengths. It is essential

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.