为第2列中值最大的第1列元素的每个组名称获取2条记录

时间:2018-11-13 09:43:57

标签: sql oracle

我有一个类似下面的表格。现在,我想获取第2列中具有最大价值的第1列元素的每个组名称的2条记录。例如,对A取85和75,对B取65和45 ... 我使用oracle数据库。 TNX

 ----------------------
 |Column 1 | Column 2 |
 ----------------------
 |    A    |    85    |
 ----------------------
 |    A    |    75    |
 ---------------------
 |    A    |    60    |
 ---------------------
 |    A    |    50    |
 ---------------------
 |    B    |    65    |
 ---------------------
 |    B    |    45    |
 ---------------------
 |    B    |    35    |
 ---------------------
 |    B    |    25    |
 ---------------------

4 个答案:

答案 0 :(得分:0)

尝试使用row_number()

image[mask != 1,:] = 0

答案 1 :(得分:0)

使用iisnode encountered an error when processing the request. HRESULT: 0x2 HTTP status: 500 HTTP subStatus: 1002 HTTP reason: Internal Server Error You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is 'true'. In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem. The last 64k of the output generated by the node.exe process to stderr is shown below: (node:4792) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. (node:4792) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. 窗口功能

row_number

答案 2 :(得分:0)

您可以使用row_number()

select t.*
from (select t.*,
             row_number() over (partition by col1 order by col2 desc) as seq 
      from table t
     ) t
where seq <= 2;

但是,fetch first . . .子句也有帮助:

select t.*
from table t
where t.col2 in (select t1.col2
                 from table t1
                 where t1.col1 = t.col1
                 order by t1.col2 desc
                 fetch first 2 rows only
                );

答案 3 :(得分:0)

尝试一下

这里是演示solution

SQL

select col1,col2 from (select col1,col2, rank() over(partition by col1 order by col2 desc) as rn from t) q where q.rn<=2;