Question

Example i having a table name 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;
Was it helpful?

Solution

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;

OTHER TIPS

Try JOIN between two tables,

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

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top