sql - 具有最大条件的 select 子句

标签 sql oracle

对于我的数据库表:

//table1
name
---------
john
mary
ali

在我的表2

//table2
title
---------
 test1
 test2
 test3

在我的表3

//table3
name      title
-----------------
 john      test1
 john      test2
 john      test3
 mary      test2

所以我的问题是找到按名称连接次数最多的标题。

所以我的查询是:

SELECT t2.title from table2 t2 inner join table3 t3 on
   t2.title = t3.title inner join table1 t1 on 
   t1.name = t3.name having max(....)

当我检查一些网站时。他们用于having子句的所有内容仅用于数字,我找不到任何适合我需要计数的问题的示例(*)

预期输出:

//result
title
---------
test2

最佳答案

按计数排序以获取从最常见标题开始的列表

SELECT t2.title 
from table2 t2 
inner join table3 t3 on t2.title = t3.title 
inner join table1 t1 on t1.name = t3.name 
group by t2.title
order by count(*) desc

要仅获取顶部结果,请使用p>

SELECT * FROM 
(
    SELECT t2.title 
    from table2 t2 
    inner join table3 t3 on t2.title = t3.title 
    inner join table1 t1 on t1.name = t3.name 
    group by t2.title
    order by count(*) desc
) X
WHERE ROWNUM = 1;

关于sql - 具有最大条件的 select 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24367311/

相关文章:

sql - SQL中交集的补充

java - 将 Java 类连接到 Derby 数据库可提高池连接的性能

sql - MS Access 2013:UNION 3个表中的任何2个有效,UNION 3个崩溃

java - 使用 CLOB 和 VARCHAR2 参数从 PL/SQL 调用 Java 函数

r - 如何使用标识列将数据从 R 追加到 Oracle 数据库表

Oracle 分析滚动百分位数

sql - 将 IIF 函数嵌套在 MAX 函数内

mysql - sql更新错误

sql - Oracle SQL - 使用合并函数

java - 使用 Oracle 和 PostgreSQL 的长文本列 hibernate