如何根据以下数据在 oracle SQL 上使用 max 函数? [关闭]
Posted
技术标签:
【中文标题】如何根据以下数据在 oracle SQL 上使用 max 函数? [关闭]【英文标题】:How to use max function on oracle SQL based on below data? [closed] 【发布时间】:2013-11-26 15:30:25 【问题描述】:假设我有以下数据并且我想收集最近的日期信息,那么使用 max 函数或任何其他函数编写 sql 的最佳方法是什么?仅供参考 - 我只有对服务器的只读访问权限,并且无法创建临时表或其他任何内容。
谢谢。
<table border="1">
<tr>
<th>NAME</th>
<th>ID</th>
<th>CODE</th>
<th>DATE</th>
</tr>
<tr>
<td>A </td>
<td>Z1 </td>
<td>780.52 </td>
<td>11/14/11</td>
</tr>
<tr>
<td>A </td>
<td>Z1 </td>
<td>780.54 </td>
<td>10/31/11</td>
</tr>
<tr>
<td>A </td>
<td>Z1 </td>
<td>780.54 </td>
<td>10/24/11 </td>
</tr>
<tr>
<td>AB </td>
<td>Z22 </td>
<td>327.23 </td>
<td>12/09/11</td>
</tr>
<tr>
<td>AB </td>
<td>Z22 </td>
<td>327.23 </td>
<td>11/02/11</td>
</tr>
<tr>
<td>AB </td>
<td>Z22 </td>
<td>327.23 </td>
<td>09/13/11</td>
</tr>
<tr>
<td>B </td>
<td>Z55 </td>
<td>327.23 </td>
<td>01/06/11</td>
</tr>
<tr>
<td>C </td>
<td>Z77 </td>
<td>327.23 </td>
<td>01/04/11</td>
</tr>
<tr>
<td>DD </td>
<td>Z888 </td>
<td>327.23 </td>
<td>10/31/11</td>
</tr>
<tr>
<td>DD </td>
<td>Z888 </td>
<td>327.23 </td>
<td>10/24/11</td>
</tr>
<tr>
<td>DD </td>
<td>Z888 </td>
<td>327.23 </td>
<td>10/06/11</td>
</tr>
<tr>
<td>DD </td>
<td>Z888 </td>
<td>327.23 </td>
<td>08/08/11</td>
</tr>
<tr>
<td>DD </td>
<td>Z888 </td>
<td>780.54 </td>
<td>07/28/11</td>
</tr>
<tr>
<td>DD </td>
<td>Z888 </td>
<td>327.23 </td>
<td>07/19/11</td>
</tr>
<tr>
<td>EE </td>
<td>Z2323 </td>
<td>327.23 </td>
<td>03/17/11</td>
</tr>
<tr>
<td>EE </td>
<td>Z2323 </td>
<td>327.23 </td>
<td>02/24/11</td>
</tr>
<tr>
<td>EE </td>
<td>Z2323 </td>
<td>780.54 </td>
<td>02/13/11</td>
</tr>
<tr>
<td>FF </td>
<td>Z99 </td>
<td>327.23 </td>
<td>07/07/11</td>
</tr>
<tr>
<td>FF </td>
<td>Z99 </td>
<td>780.54 </td>
<td>06/28/11</td>
</tr>
<tr>
<td>II </td>
<td>Z963 </td>
<td>327.23 </td>
<td>09/19/11</td>
</tr>
<tr>
<td>II </td>
<td>Z963 </td>
<td>327.23 </td>
<td>08/30/11</td>
</tr>
<tr>
<td>II </td>
<td>Z963 </td>
<td>327.23 </td>
<td>06/29/11</td>
</tr>
<tr>
<td>II </td>
<td>Z963 </td>
<td>780.54 </td>
<td>06/29/11</td>
</tr>
<tr>
<td>II </td>
<td>Z963 </td>
<td>780.54 </td>
<td>06/14/11</td>
</tr>
<tr>
<td>L </td>
<td>Z99999 </td>
<td>327.23 </td>
<td>09/16/11</td>
</tr>
<tr>
<td>NN </td>
<td>Z9870 </td>
<td>327.23 </td>
<td>11/23/11</td>
</tr>
<tr>
<td>NN </td>
<td>Z9870 </td>
<td>327.23 </td>
<td>10/06/11</td>
</tr>
<tr>
<td>NN </td>
<td>Z9870 </td>
<td>327.23 </td>
<td>06/07/11</td>
</tr>
<tr>
<td>NN </td>
<td>Z9870 </td>
<td>780.54 </td>
<td>01/18/11</td>
</tr>
<tr>
<td>NN </td>
<td>Z9870 </td>
<td>780.54 </td>
<td>01/11/11</td>
</tr>
</table>
【问题讨论】:
最近的什么?姓名?身份证总体?第一名?前10名?您也需要代码还是只需要数据?请为上述数据添加预期输出。 @OGHaza 嗯,我明白了。我没有看到的是任何预期的输出。我假设SELECT TOP 1 date FROM table
不是正确答案。
最近的总体还是最近的每个代码/名称/ID?
How to get the last row in a table - Oracle 11g? 的可能重复项
Mike - 最近的每个代码、名称、ID 所有三个。
【参考方案1】:
我认为您可以尝试以下任何一种方法:
select * from (Select * from your_table order by date desc) where rownum=1;
select * FROM your_table WHERE date= (SELECT MAX(date) from your_table)
SELECT * FROM (select a.*, MAX(date) OVER() max_date_column FROM your_table a) WHERE your_date_column = max_date_column
SELECT * FROM( select a.*, ROW_NUMBER OVER(ORDER BY date DESC) rn FROM your_table a) WHERE rn = 1
【讨论】:
如果你有Id作为主键那么你可以试试,select * from tablename where id in (Select id from tablename order by date desc) and rownum=1;【参考方案2】:SQL Fiddle
查询 1:
SELECT MIN( NAME ) KEEP ( DENSE_RANK LAST ORDER BY "DATE" ASC ) AS NAME,
MIN( ID ) KEEP ( DENSE_RANK LAST ORDER BY "DATE" ASC ) AS ID,
MIN( CODE ) KEEP ( DENSE_RANK LAST ORDER BY "DATE" ASC ) AS CODE,
MIN("DATE") KEEP ( DENSE_RANK LAST ORDER BY "DATE" ASC ) AS "DATE"
FROM tbl
Results:
| NAME | ID | CODE | DATE |
|------|------|--------|---------------------------------|
| AB | Z22 | 327.23 | December, 09 2011 00:00:00+0000 |
【讨论】:
【参考方案3】:SELECT tbl.Name, tbl.Id, tbl.Code, tbl.date
FROM tbl
JOIN (
SELECT Name, Id, Code, MAX(date) AS Max_Date
FROM tbl
GROUP BY Name, Id, Code) tbl_max
ON tbl.Name=tbl_max.Namne
AND tbl.Id=tbl_max.Id
And tbl.Code=tbl_Max.Code
AND tbl.Date=tbl_max.Max_Date
【讨论】:
其实我想要每个名字、id和代码的最新记录。 我已经编辑了答案,以按名称、ID 和代码提供最新记录,而不仅仅是 Id。 非常感谢先生!以上是关于如何根据以下数据在 oracle SQL 上使用 max 函数? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SQL 2000/2005 上设置与 Oracle 数据库的链接服务器?