无法 MAX(COUNT) 并且有多个列
Unable to MAX(COUNT) and have multiple columns
我有 4 个 table,EMPLOYEE,DRIVER,TRIP 和 TRIPLEG
EMPLOYEE table 有我想提取并显示最大计数的名称,有与 DRIVER 共享的 E#
DRIVER table 有 L#(许可证号),与 TRIP
通用
TRIP table 具有与 TRIPLEG
相同的 T#(行程编号)
我正在尝试查找 driver 完成的最大三联体数量(在本例中 driver 许可证号为 10002:
SELECT MAX(COUNT(TRIPLEG.LEG#))
FROM TRIP, TRIPLEG
ON TRIP.T# = TRIPLEG.T#
WHERE TRIP.L# = 10002
GROUP BY TRIP.T#
COUNT(TRIPLEG.LEG#) 给我 https://i.imgur.com/AYAovov.png,
所以我做了上面的 MAX(COUNT(TRIPLEG.LEG#)) 这给了我这个:https://i.imgur.com/alCFlO3.png
我无法继续,因为我尝试选择更多列(TRIP.T#),例如
SELECT TRIP.T#, MAX(COUNT(TRIPLEG.LEG#))
FROM TRIP, TRIPLEG
ON TRIP.T# = TRIPLEG.T#
WHERE TRIP.L# = 10002
GROUP BY TRIP.T#
给我一个错误:ORA-00937: 不是 single-group 组函数
有什么建议吗?在我加入更多 table 之前,需要能够从小处着手并进行选择,以使员工姓名显示在最大三边形计数
旁边
提前致谢
基本上我想要这样的东西:(只有 1 行,这是完成的 MAX 三联体 (5))
NAME MAX(COUNT(TRIPLEG.LEG#))
-----------------------------------
BOB 5
根据您的表格查找 driver 已完成的三联体的最大数量(在本例中 driver 许可证号为 10002:
Select a.tripno, max(a.trips)
From
(Select Trip.T# as tripno, count(tripleg.leg#) as trips
From tripleg join trip on tripleg.T# = trip.T#
where trip.L# = 10002
Group by tripno
) a
Group by a.tripno
我没有你的 table,所以我将使用 Scott 的 EMP 和 DEPT(因为你使用 Oracle,所以我想你对它们很熟悉)。
这个有效:
SQL> select d.dname, count(e.empno) cnt
2 from emp e join dept d on e.deptno = d.deptno
3 where d.deptno in (10, 20)
4 group by d.dname;
DNAME CNT
-------------- ----------
ACCOUNTING 3
RESEARCH 5 --> MAX count is this
SQL>
如果 SELECT
列列表中没有其他列(您已经知道),并且它 returns 所需值:
,则嵌套计数有效
SQL> select max(count(e.empno)) cnt
2 from emp e join dept d on e.deptno = d.deptno
3 where d.deptno in (10, 20)
4 group by d.dname;
CNT
----------
5
SQL>
但是,这行不通(你也知道):
select d.dname, max(count(e.empno)) cnt
from emp e join dept d on e.deptno = d.deptno
where d.deptno in (10, 20)
group by d.dname;
要修复它,请使用 CTE(Common Table Expression a.k.a。WITH
factoring clause)或内联视图;我将向您展示第一个选项,还有另一个补充:我将 rank 计数并找到 "highest" 一个,稍后将其用于 select所需的行。
SQL> with tcnt as
2 (select d.deptno,
3 d.dname,
4 count(e.empno) cnt,
5 rank() over (order by count(e.empno) desc) rnk --> rank them DESC
6 from emp e join dept d on e.deptno = d.deptno
7 where d.deptno in (10, 20)
8 group by d.dname, d.deptno
9 )
10 select t.deptno, t.dname, t.cnt
11 from tcnt t
12 where rnk = 1; --> fetch only row(s) with highest rank
DEPTNO DNAME CNT
---------- -------------- ----------
20 RESEARCH 5
SQL>
最后,从其他 table 中添加更多列:
SQL> with tcnt as
2 (select d.deptno,
3 d.dname,
4 count(e.empno) cnt,
5 rank() over (order by count(e.empno) desc) rnk
6 from emp e join dept d on e.deptno = d.deptno
7 where d.deptno in (10, 20)
8 group by d.dname, d.deptno
9 )
10 select t.deptno, t.dname, t.cnt, e.ename, e.job
11 from tcnt t join emp e on e.deptno = t.deptno
12 where rnk = 1;
DEPTNO DNAME CNT ENAME JOB
---------- -------------- ---------- ---------- ---------
20 RESEARCH 5 SMITH CLERK
20 RESEARCH 5 JONES MANAGER
20 RESEARCH 5 SCOTT ANALYST
20 RESEARCH 5 ADAMS CLERK
20 RESEARCH 5 FORD ANALYST
SQL>
我有 4 个 table,EMPLOYEE,DRIVER,TRIP 和 TRIPLEG
EMPLOYEE table 有我想提取并显示最大计数的名称,有与 DRIVER 共享的 E# DRIVER table 有 L#(许可证号),与 TRIP
通用TRIP table 具有与 TRIPLEG
相同的 T#(行程编号)我正在尝试查找 driver 完成的最大三联体数量(在本例中 driver 许可证号为 10002:
SELECT MAX(COUNT(TRIPLEG.LEG#))
FROM TRIP, TRIPLEG
ON TRIP.T# = TRIPLEG.T#
WHERE TRIP.L# = 10002
GROUP BY TRIP.T#
COUNT(TRIPLEG.LEG#) 给我 https://i.imgur.com/AYAovov.png,
所以我做了上面的 MAX(COUNT(TRIPLEG.LEG#)) 这给了我这个:https://i.imgur.com/alCFlO3.png
我无法继续,因为我尝试选择更多列(TRIP.T#),例如
SELECT TRIP.T#, MAX(COUNT(TRIPLEG.LEG#))
FROM TRIP, TRIPLEG
ON TRIP.T# = TRIPLEG.T#
WHERE TRIP.L# = 10002
GROUP BY TRIP.T#
给我一个错误:ORA-00937: 不是 single-group 组函数
有什么建议吗?在我加入更多 table 之前,需要能够从小处着手并进行选择,以使员工姓名显示在最大三边形计数
旁边提前致谢
基本上我想要这样的东西:(只有 1 行,这是完成的 MAX 三联体 (5))
NAME MAX(COUNT(TRIPLEG.LEG#))
-----------------------------------
BOB 5
根据您的表格查找 driver 已完成的三联体的最大数量(在本例中 driver 许可证号为 10002:
Select a.tripno, max(a.trips)
From
(Select Trip.T# as tripno, count(tripleg.leg#) as trips
From tripleg join trip on tripleg.T# = trip.T#
where trip.L# = 10002
Group by tripno
) a
Group by a.tripno
我没有你的 table,所以我将使用 Scott 的 EMP 和 DEPT(因为你使用 Oracle,所以我想你对它们很熟悉)。
这个有效:
SQL> select d.dname, count(e.empno) cnt
2 from emp e join dept d on e.deptno = d.deptno
3 where d.deptno in (10, 20)
4 group by d.dname;
DNAME CNT
-------------- ----------
ACCOUNTING 3
RESEARCH 5 --> MAX count is this
SQL>
如果 SELECT
列列表中没有其他列(您已经知道),并且它 returns 所需值:
SQL> select max(count(e.empno)) cnt
2 from emp e join dept d on e.deptno = d.deptno
3 where d.deptno in (10, 20)
4 group by d.dname;
CNT
----------
5
SQL>
但是,这行不通(你也知道):
select d.dname, max(count(e.empno)) cnt
from emp e join dept d on e.deptno = d.deptno
where d.deptno in (10, 20)
group by d.dname;
要修复它,请使用 CTE(Common Table Expression a.k.a。WITH
factoring clause)或内联视图;我将向您展示第一个选项,还有另一个补充:我将 rank 计数并找到 "highest" 一个,稍后将其用于 select所需的行。
SQL> with tcnt as
2 (select d.deptno,
3 d.dname,
4 count(e.empno) cnt,
5 rank() over (order by count(e.empno) desc) rnk --> rank them DESC
6 from emp e join dept d on e.deptno = d.deptno
7 where d.deptno in (10, 20)
8 group by d.dname, d.deptno
9 )
10 select t.deptno, t.dname, t.cnt
11 from tcnt t
12 where rnk = 1; --> fetch only row(s) with highest rank
DEPTNO DNAME CNT
---------- -------------- ----------
20 RESEARCH 5
SQL>
最后,从其他 table 中添加更多列:
SQL> with tcnt as
2 (select d.deptno,
3 d.dname,
4 count(e.empno) cnt,
5 rank() over (order by count(e.empno) desc) rnk
6 from emp e join dept d on e.deptno = d.deptno
7 where d.deptno in (10, 20)
8 group by d.dname, d.deptno
9 )
10 select t.deptno, t.dname, t.cnt, e.ename, e.job
11 from tcnt t join emp e on e.deptno = t.deptno
12 where rnk = 1;
DEPTNO DNAME CNT ENAME JOB
---------- -------------- ---------- ---------- ---------
20 RESEARCH 5 SMITH CLERK
20 RESEARCH 5 JONES MANAGER
20 RESEARCH 5 SCOTT ANALYST
20 RESEARCH 5 ADAMS CLERK
20 RESEARCH 5 FORD ANALYST
SQL>