需要编写查询以显示职员的全名专业名称,并将新列命名为专业

时间:2019-04-07 09:23:10

标签: sql oracle

输入查询以显示职员全名,即拥有一名以上专业的职员的所有男性的专业数目。 命名新的专业列号。 按员工全名排序

Table Describ
SQL> describ staff_speciality;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 STAFFID                                   NOT NULL CHAR(2)
 SPECID                                             CHAR(4)
 DATEQUALIFIED                             NOT NULL DATE
 VALIDDATE                                          DATE
 DETAILS                                            VARCHAR2(100)

SQL> describ staff
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 STAFFID                                   NOT NULL CHAR(2)
 FIRSTNAME                                 NOT NULL VARCHAR2(20)
 LASTNAME                                  NOT NULL VARCHAR2(20)
 ROLE                                               VARCHAR2(15)
 GENDER                                             CHAR(1)
 DATEJOINED                                NOT NULL DATE
 DATELEFT                                           DATE


COLUMN count(staffid) HEADING "Num of Specialities"
SELECT firstname || ' '|| lastname "Staff Full Name",
count(staffid) > 1
FROM staff, staff_speciality
ORDER By Staff Full Name;


COLUMN count(staffid) HEADING "Num of Specialities"
SELECT firstname || ' '|| lastname "Staff Full Name",
count(staffid) > 1
FROM staff, staff_speciality
ORDER By Staff Full Name;

Showing firstname and last name as Staff Full name
than counting how many staff with more than 1 entry
and sorting it by Staff Full name

2 个答案:

答案 0 :(得分:0)

您可以使用让count(..)> 1并按

分组
SELECT firstname || ' '|| lastname "Staff Full Name",
count(staffid) "Num of Specialities"
FROM staff, staff_speciality
GROUP "Staff Full Name"
HAVING count(staffid)> 1
ORDER By "Staff Full Name";

答案 1 :(得分:0)

您可以在表和组之间使用INNER JOIN来获得特殊数量。
最后,通过HAVING,您可以将结果限制为具有1个以上专业的工作人员:

select 
  t.lastname || ' '|| t.firstname "Staff Full Name",
  count(s.specid) "Num of Specialities"
from staff t inner join staff_speciality s
on s.staffid = t.staffid
where t.gender = 'M'
group by t.staffid, t.lastname || ' '|| t.firstname
having count(s.specid) > 1
order by t.lastname || ' '|| t.firstname