ORACLE-在LOOP(或类似)中查找具体结果 [英] ORACLE - find a concrete result inside a LOOP (OR SIMILAR)

查看:62
本文介绍了ORACLE-在LOOP(或类似)中查找具体结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Oracle的新手,我正在编写一些东西,但是我需要做一些超越我的事情.

I'm newbie in Oracle and I am programming some stuff things but I need to do something that it's beyond me.

首先,我必须使用Oracle 8i

First of all, I have to use Oracle 8i

我们从一个表开始,该表的数据类似于:

We start with a table with data similar to:

Column1  Column2
      A        7
      A        9
      A       13
      B        5
      B        6
      C        1
      C        4
      C        9
      C       40

我需要从此表中获取每个字母(column1)的前2个值

I need to take FIRST 2 values from this table for EACH letter (column1)

值1 = n1值2 = n2

Value1 = n1 Value2 = n2

例如,对于A而言,其值为7和9,而对于B而言,其值为5和6

For example, for A values are 7 and 9 but for B are 5 and 6

对不起,我不能画一张桌子(

Sorry, I can't draw a table (

而且我不知道该表ofc可以有多少个字母,所以我认为最好的方法可能是所有表都为一个LOOP,只占用第一行和第二行(表已排好,并且总是需要第一个和第二个值用于每个字母),但我不确定该怎么办...

And I don't know how many letter could have this table ofc, so I suppose the best way it's maybe a LOOP for all table, taking just first and second row (table is arranged and ALWAYS need first and second values for each letter) but I'm not sure how can I do...

也许是按column1和rownum< 3进行的查询过滤器?我不确定此查询是否始终仅采用前2个值或采用2个偶然值...

Maybe a query filter by column1 and a rownum <3 ? I'm not sure if this query always take just first 2 values or take 2 aleatory values...

推荐答案

您可以为此使用窗口函数.

You can use a window function for this.

select column_1, column_2
from (
   select column_1, column_2,
          row_number() over (partition by column_1 order by column_2) as rn
   from the_table
) t
where rn <= 2;

您可以在 greatest-每组n个

这篇关于ORACLE-在LOOP(或类似)中查找具体结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆