我有两个表,Staff和Cust_Order。我想在staff表中添加“First name”列 同时仍然执行以下代码:
Select Staff_No Count(*) AS "Number Of Orders"
From Cust_Order
Group by Staff_No;
由于
答案 0 :(得分:0)
SELECT DISTINCT
STAFF_NO,
FIRST_NAME,
COUNT (*) OVER (PARTITION BY STAFF_NO) AS "Number Of Orders"
FROM CUST_ORDER;
使用不同,因为如果first_name不唯一,可能会出现重复结果。
答案 1 :(得分:0)
将计数作为子查询
Select First_Name,
(Select Count(*) From Cust_Order o
Where o.STAFF_NO = Cust_Order.STAFF_NO) "Number Of Orders"
From Cust_Order
答案 2 :(得分:0)
加入订单和员工表,然后分组将需要包含其他列
SELECT
co.Staff_No
, s.First_name
, COUNT(*) AS "Number Of Orders"
FROM Cust_Order co
INNER JOIN Staff s on co.Staff_No = s.Staff_No
GROUP BY
co.Staff_No
, s.First_name
;