具有特定列名SQL的GROUP BY

时间:2014-04-26 09:10:33

标签: sql oracle scripting

示例i具有表名DEPARTMENT

//DEPARTMENT
D#
--------
1
2
3

现在有一个项目表

//Project
P#      D#
-----------
1       1
2       1
3       2
4       1

那么当使用提示显示所有信息时,我应该如何使用具有特定列名的组,它应该是类似

Enter Department Number : 1

D#      total project
---------------------
1            3

到目前为止,我这样做了

ACCEPT dno PROMPT 'Enter Department Number: ' 
SELECT DNAME FROM DEPARTMENT WHERE D#=&dno;
SELECT count(*) from PROJECT where D#=&dno;

2 个答案:

答案 0 :(得分:0)

使用此

select DNAME, count(Project) AS "total project" FROM DEPARTMENT d, PROJECT p
WHERE d.dno=p.dno and D.DNO=&dno  group by DNAME;

仅获得总数

select  count(Project) AS "total project" FROM DEPARTMENT d, PROJECT p
WHERE d.dno=p.dno and D.DNO=&dno  group by DNAME;

答案 1 :(得分:0)

尝试两个表之间的JOIN,

ACCEPT dno PROMPT 'Enter Department Number: ' 
SELECT DNAME, count(*)
    FROM DEPARTMENT d, PROJECT p
    WHERE d.dno=p.dno and d.dno=&dno
    group by d.DNAME;

修改

显示dnoDNAME以及项目数

ACCEPT dno PROMPT 'Enter Department Number: ' 
SELECT d.dno,DNAME, count(*) as 'total'
    FROM DEPARTMENT d, PROJECT p
    WHERE d.dno=p.dno and d.dno=&dno
    group by d.dno,DNAME;