简体   繁体   English

具有特定列名SQL的GROUP BY

[英]GROUP BY with specific column name SQL

Example i having a table name DEPARTMENT 示例我的表名称为DEPARTMENT

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

Now having a project table 现在有一个项目表

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

So how should i use the group by with specific column name when display out all the information using prompt, it should be something like 因此,在使用提示符显示所有信息时,我应如何使用具有特定列名的分组依据?

Enter Department Number : 1

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

So far i done like this 到目前为止,我已经做到了

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

use this 用这个

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

to get total only 只求总数

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

Try JOIN between two tables, 尝试在两个表之间进行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;

EDIT 编辑

Displaying dno and DNAME along with count of projects 显示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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM