查询获取数据

发布于 2024-10-15 15:05:52 字数 652 浏览 1 评论 0原文

我是 SQL 新手,需要编写一个看起来很复杂的查询。 我需要编写一个查询来从表中获取回报,如下所示:

 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   

这是我需要从上表中选择的内容:-

  • id,
  • Price_date(本月最后价格的日期),
  • return((last_price_of_month/last_price_of_previous_month) - 1)、
  • last_date_for_return_calculation(下个月的第一天)

示例数据如下:-

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

有人可以帮我解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-10-22 15:05:52

根据您的要求,您的日期似乎是 2011 年,而不是 2010 年。此外,我在一月份添加了一行,以确保获得样本数据上个月的最后一行。

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

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
脸赞 2024-10-22 15:05:52

通过查询就可以实现。

但是,如果您只是像这样查询表并在程序端过滤它,您可能会更好。

It's possible with a query.

But you're probably better of if you just query the table like that and filter it on your program side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文