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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:52:26+00:00 2026-06-01T04:52:26+00:00

I have a dataset of securities prices in a database. The data is structured

  • 0

I have a dataset of securities prices in a database. The data is structured like this:

id      security_id     time_to_maturity      price
001         01               1.5               100.45
002         01               1.3               101.45
003         01               1.1               102.45
004         01               1.02              101.45
005         01               1.0               101.45
006         03              22.3               94.45
007         03              22.1               96.45
008         03              21.8               98.45
009         05               4.2               111.45
010         05               4.1               112.45
011         05               3.8               111.45
...

id is the row_id and security_id is the id of each security. I am trying to get only data from a certain time range for each security. First I run a query to find the min and max for each security id, then find the difference between the min and max and finally find a value that is 10% more than the minimum like this:

SELECT security_id, MIN(time_to_maturity), MAX(time_to_maturity),
    MAX(time_to_maturity) - MIN(time_to_maturity) tDiff,
    ((MAX(time_to_maturity) - MIN(time_to_maturity)) * .1) + MIN(time_to_maturity)
  FROM db1
  group by security_id
  order by security_id

This gives me the following:

security_id    min()     max()     diff      min+(diff*.1)
  01             1.0        1.5      .5            1.05
  03            21.8       22.3      .5           21.85
  05             3.8        4.2      .4            3.84

Finally what I’d like to do is select from the main data set only those rows for each security_id where the time_to_maturity is < min+(diff*.1).

I’m not sure how to structure it though as I feel like I need a loop to subset data by security_id, then by time_to_maturity is < min+(diff*.1).

The answer would look something like this:

id      security_id     time_to_maturity      price
004         01               1.02              101.45
005         01               1.0               101.45
008         03              21.8               98.45
011         05               3.8               111.45

Any suggestions?

  • 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-01T04:52:27+00:00Added an answer on June 1, 2026 at 4:52 am
    SELECT A.id,B.security_id,A.time_to_maturity,A.price
    FROM db1 A,
    (
    SELECT security_id, MIN(time_to_maturity) AS min_time_to_maturity, MAX(time_to_maturity) AS max_time_to_maturity,
        MAX(time_to_maturity) - MIN(time_to_maturity) tDiff,
        ((MAX(time_to_maturity) - MIN(time_to_maturity)) * .1) + MIN(time_to_maturity)
      FROM db1
      group by security_id
      order by security_id
    ) B
    WHERE A.security_id = B.security_id
      AND A.time_to_maturity < (B.min_time_to_maturity+(B.tdiff*0.1));
    

    PS:This will work only in MYSQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataset like this: Year MM DD HH 158 2010 7 1
I have a DataSet consisting of XML data, I can easily output this to
i have dataset that contain data. i need to run any query on this
I have a dataset something like this: col_a col_b col_c 1 abc_boy 1 2
I have a DataSet with some Datatables and I am saving this DataSet as
I have a dataset that returns questions and answers from the database, each answer
I have a dataset of 1 minute data of 1000 stocks since 1998, that
I have this dataset for income: Income Number of people 0 245981 8.8 150444
I have a dataset akin to this User Date Value A 2012-01-01 4 A
I have a dataset that points to a View in my SQL database, and

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.