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

  • Home
  • SEARCH
  • 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 3855714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:49:06+00:00 2026-05-19T17:49:06+00:00

I am a newbee to SQL and need to write a query which seems

  • 0

I am a newbee to SQL and need to write a query which seems to be complex.
I need to write a query for getting the returns from a table which looks like:

 id    Price_date    price   
 1     1-1-2010     20    
 1     2-2-2010     21    
 1     7-2-2010     22    
 1     27-2-2010    23    
 1     3-3-2010     23   

Here is what I need to select from the above table:-

  • id,
  • price_date(date of last price of month),
  • return((last_price_of_month/last_price_of_previous_month) -1),
  • last_date_for_return_calculation(first day of next month)

And the sample data would be like:-

    id      price_date   return        last_date_for_return_calculation    
    1       27-2-2010    (23/20 -1)    1-3-2011     

Could somebody help me in solving this problem?

  • 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-19T17:49:07+00:00Added an answer on May 19, 2026 at 5:49 pm

    based on your requirement, it seems your date are in 2011, not 2010. Also I added a row in january to be sure to get the last row of the previous month of the sample data.

    SQL> WITH DATA AS (
      2  SELECT 1 id, to_date('1-1-2011', 'dd-mm-yyyy') Price_date, 19.5 price
      3    FROM DUAL
      4  UNION ALL SELECT 1, to_date('31-1-2011', 'dd-mm-yyyy'), 20 FROM DUAL
      5  UNION ALL SELECT 1, to_date('2-2-2011', 'dd-mm-yyyy'), 21 FROM DUAL
      6  UNION ALL SELECT 1, to_date('7-2-2011', 'dd-mm-yyyy'), 22 FROM DUAL
      7  UNION ALL SELECT 1, to_date('27-2-2011', 'dd-mm-yyyy'), 23 FROM DUAL
      8  UNION ALL SELECT 1, to_date('3-3-2011', 'dd-mm-yyyy'), 23 FROM DUAL
      9  )
     10  SELECT ID,
     11         MAX(price_date) price_date,
     12         MAX(price) KEEP (DENSE_RANK FIRST ORDER BY price_date DESC)
     13         / MAX(price)
     14           KEEP (DENSE_RANK FIRST
     15                 ORDER BY CASE WHEN price_date < trunc(SYSDATE, 'month')
     16                               THEN price_date END
     17                 DESC NULLS LAST) - 1 RETURN,
     18         add_months(trunc(SYSDATE, 'month'), 1) last_date_for_return_calc
     19    FROM DATA
     20   WHERE price_date >= add_months(trunc(SYSDATE, 'month'), -1)
     21     AND price_date < add_months(trunc(SYSDATE, 'month'), 1)
     22   GROUP BY ID;
    
            ID PRICE_DATE      RETURN LAST_DATE_FOR_RETURN_CALC
    ---------- ----------- ---------- -------------------------
             1 27/02/2011        0,15 01/03/2011
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to import data from Excel into a SQL 2000 db. I need
I need some help with make a datatable from sql. I'm a Newbie. I
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
Newbie to LINQ, and trying to write the following query... select f.Section_ID, f.Page_ID, f.SortOrder,
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
This is a sql newbie question. Basically, I want an extra column to return
This is sort of SQL newbie question, I think, but here goes. I have
I'm a newbie when it comes to SQL. When creating a stored procedure with
I'm still a newbie to Adobe Air/Flex, and still fairly new with SQL. I've
.NET newbie here... I'd like to make a button in a Windows form that

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.