oracle SQL -如何选择具有最大值的列的行[重复]

yebdmbv4  于 2023-05-28  发布在  Oracle
关注(0)|答案(9)|浏览(140)

此问题已在此处有答案

How do I limit the number of rows returned by an Oracle query after ordering?(15个回答)
11个月前关闭。

date                 value

18/5/2010, 1 pm        40
18/5/2010, 2 pm        20
18/5/2010, 3 pm        60
18/5/2010, 4 pm        30
18/5/2010, 5 pm        60
18/5/2010, 6 pm        25

我需要查询具有max(value)的行(即60).所以,这里我们得到两行。从中,我需要当天时间戳最小的行(即18/5/2010,下午3点-> 60)

hgqdbh6s

hgqdbh6s1#

像TOP、LIMIT、ROWNUM等关键字是依赖于数据库的。请阅读这篇文章了解更多信息。
http://en.wikipedia.org/wiki/Select_(SQL)#Result_limits
Oracle:可以使用ROWNUM。

select * from (select * from table 
order by value desc, date_column) 
where rownum = 1;

更具体地回答这个问题:

select high_val, my_key
from (select high_val, my_key
      from mytable
      where something = 'avalue'
      order by high_val desc)
where rownum <= 1
yshpjwxd

yshpjwxd2#

分析!这避免了必须访问表两次:

SELECT DISTINCT
       FIRST_VALUE(date_col)  OVER (ORDER BY value_col DESC, date_col ASC),
       FIRST_VALUE(value_col) OVER (ORDER BY value_col DESC, date_col ASC)
FROM   mytable;
xxhby3vn

xxhby3vn3#

答案是添加子选择:

SELECT [columns]
FROM table t1
WHERE value= (select max(value) from table)
AND date = (select MIN(date) from table t2 where t1.value = t2.value)

这应该可以工作,并摆脱了在日期子句中有额外子选择的必要性。

6yt4nkrj

6yt4nkrj4#

SQL> create table t (mydate,value)
  2  as
  3  select to_date('18/5/2010, 1 pm','dd/mm/yyyy, hh am'), 40 from dual union all
  4  select to_date('18/5/2010, 2 pm','dd/mm/yyyy, hh am'), 20 from dual union all
  5  select to_date('18/5/2010, 3 pm','dd/mm/yyyy, hh am'), 60 from dual union all
  6  select to_date('18/5/2010, 4 pm','dd/mm/yyyy, hh am'), 30 from dual union all
  7  select to_date('18/5/2010, 5 pm','dd/mm/yyyy, hh am'), 60 from dual union all
  8  select to_date('18/5/2010, 6 pm','dd/mm/yyyy, hh am'), 25 from dual
  9  /

Table created.

SQL> select min(mydate) keep (dense_rank last order by value) mydate
  2       , max(value) value
  3    from t
  4  /

MYDATE                   VALUE
------------------- ----------
18-05-2010 15:00:00         60

1 row selected.

你好罗伯

exdqitrt

exdqitrt5#

从技术上讲,这与@Sujee的答案相同。这也取决于您的Oracle版本是否正常工作。(我认为这种语法是在Oracle 12中引入的。?)

SELECT *
FROM   table
ORDER BY value DESC, date_column ASC
FETCH  first 1 rows only;

正如我所说,如果你看看引擎盖下,我认为这段代码是由Oracle优化器内部解压缩的,读起来像@Sujee的。然而,我对漂亮的编码很着迷,没有好的理由嵌套select语句并不算漂亮!!:-P

bgtovc5b

bgtovc5b6#

在Oracle中:
这将根据范围获取表中max(high_瓦尔)的键。

select high_val, my_key
from (select high_val, my_key
      from mytable
      where something = 'avalue'
      order by high_val desc)
where rownum <= 1
cpjpxq1n

cpjpxq1n7#

在Oracle DB中:

create table temp_test1 (id number, value number, description varchar2(20));

insert into temp_test1 values(1, 22, 'qq');
insert into temp_test1 values(2, 22, 'qq');
insert into temp_test1 values(3, 22, 'qq');
insert into temp_test1 values(4, 23, 'qq1');
insert into temp_test1 values(5, 23, 'qq1');
insert into temp_test1 values(6, 23, 'qq1');

SELECT MAX(id), value, description FROM temp_test1 GROUP BY value, description;

Result:
    MAX(ID) VALUE DESCRIPTION
    -------------------------
    6         23    qq1
    3         22    qq
eqfvzcg8

eqfvzcg88#

最简单的答案是
--设置名为“t1”的测试表

create table t1
(date datetime,
value int)

--加载数据。--注:日期格式与问题中不同

insert into t1
Select '5/18/2010 13:00',40
union all
Select '5/18/2010 14:00',20
union all
Select '5/18/2010 15:00',60 
union all
Select '5/18/2010 16:00',30 
union all
Select '5/18/2010 17:00',60 
union all
Select '5/18/2010 18:00',25

--查找具有最大数量和最小日期的行。

select *
from t1
where value = 
    (select max(value)  from t1)
and date = 
    (select min(date) 
    from t1
    where value = (select max(value)  from t1))

我知道你可以做“TOP 1”的答案,但通常你的解决方案变得足够复杂,你不能使用它的一些原因。

7ajki6be

7ajki6be9#

可以使用此函数,ORACLE DB

public string getMaximumSequenceOfUser(string columnName, string tableName, string username)
    {
        string result = "";
        var query = string.Format("Select MAX ({0})from {1} where CREATED_BY = {2}", columnName, tableName, username.ToLower());

        OracleConnection conn = new OracleConnection(_context.Database.Connection.ConnectionString);
        OracleCommand cmd = new OracleCommand(query, conn);
        try
        {
            conn.Open();
            OracleDataReader dr = cmd.ExecuteReader();
            dr.Read();
            result = dr[0].ToString();
            dr.Dispose();
        }
        finally
        {
            conn.Close();
        }
        return result;
    }

相关问题