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 8461865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:59:54+00:00 2026-06-10T13:59:54+00:00

Consider the following table ( portfolio ). It is a transaction log for a

  • 0

Consider the following table (portfolio). It is a transaction log for a stock-market investor. Each day, he either buys, sells or holds (previously bought stocks which are not yet being sold) stocks (identified by sp100_id):

_date       sp100_id  action  price
-----------------------------------
2011-03-21  11        buy     10.50
2011-03-21  55        buy     60.00
2011-03-21  99        buy      5.15
2011-03-22  11        sell     9.80
2011-03-22  55        sell    61.50
2011-03-22  99        hold     5.60
2011-03-23   1        buy     95.00
2011-03-23   2        buy     25.60
2011-03-23  99        hold
2011-03-24   1        sell    96.00
2011-03-24   2        hold
2011-03-24  99        hold
2011-03-25  11        buy      8.90
2011-03-25   2        sell    28.00
2011-03-25  99        hold

The log stops at 2011-03-25. For 2011-03-26, I want to know:
– what stocks are still left in the portfolio
– for what price and on what date these stocks were orginally bought

If we do this manually:
– stock 11 is bought on 2011-03-21, sold on 2011-03-22, but bought again on 2011-3-25 for 8.90 and we haven’t sold it since, so it is still in portfolio on 2011-03-26
– stock 55 is bought on 2011-03-21 and sold on 2011-03-22 so not in portfolio anymore
– stock 99 is bought on 2011-03-21 and we have held it and never sold so it is still in portfolio on 2011-03-26 for a price of 5.15
– stock 1 and 2 are both bought and sold before 2011-03-26

So the portfolio on 2011-03-26 consists of:

sp100_id  buy_date    buy_price
-------------------------------
11        2011-03-25  8.90
99        2011-03-21  5.15

My question is: with what query can the above output be returned from the table?

SQLFiddle here

  • 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-06-10T13:59:56+00:00Added an answer on June 10, 2026 at 1:59 pm
    select t1.sp100_id, t1._date as buy_date, t1.price
    from (select * from portfolio where action='buy')  t1
        left join (select * from portfolio where action='sell') t2 
          on t1.sp100_id=t2.sp100_id
        and t1._date<t2._date
    where t2.sp100_id is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Consider following MySQL table: CREATE TABLE `log` ( `what` enum('add', 'edit', 'remove') CHARACTER SET
Consider the following table: stock _date return2 bullishness ----------------------------------------- 1 2011-03-14 0.10 0.75 2
Consider the following table of tweets id tweet ------------------------------------------------------ 1 alcoa inc stock analysis
Consider the following table which has the fields - id (int) and date_created (datetime):
Consider the following table.... hotel facilities 1 internet 1 swimming pool 1 wi-fi 1
Consider the following table tweets tweet_id call_id id_str timestamp text -------------------------------------------------- 1 11 12345
Consider the following table views user_id _date cnt ------------------------ 1 2011-02-10 123 1 2011-02-11
Consider the following table: msg_id _time ------------- 1 13:32 2 13:56 3 14:05 4
Consider the following DB table: c p ========= 1 'a' 1 'b' 2 'a'

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.