我的任务是选择推销员的姓名和姓名,并计算他们在FireBird 2.5中在2010年销售的汽车数量。这是数据库:
Salesman(salesmanID:int primary key, salesmanName:char(20), SalesmanSurname:char(20))
sale (SalesDate:Date, SalesmanID:foreignkey to salesman)
我试过这个:
select s.count(*), sm.salesmanID, sm.salesmanName, sm.SalesmanSurname
from sale s inner join salesman on s.salesmanID=sm.salesmanID
where SalesDate between '1.1.2010' and '31.12.2010';
我得到的错误是:
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause)
答案 0 :(得分:1)
以这种方式工作
SELECT
count(*),
sm.salesmanID,
sm.salesmanName,
sm.SalesmanSurname
FROM
sale s
INNER JOIN salesman sm ON s.salesmanID = sm.salesmanID
WHERE
s.SalesDate BETWEEN '1.1.2010'
AND '31.12.2010'
GROUP BY
sm.salesmanID,
sm.salesmanName,
sm.SalesmanSurname
答案 1 :(得分:0)
您使用的s.count(*)
不是有效的语法。这可能是您收到错误的原因之一。
另外,我不会在任何表格上看到sm
作为别名。这里发现了很多问题。
答案 2 :(得分:0)
首先制作聚合(你错过了一个组),然后将结果与推销员表联系起来:
select s.salescount, s.salesmanID , sm.salesmanName, sm.SalesmanSurname from
(select count(*) as salescount, salesmanID
FROM sale where SalesDate between '1.1.2010' and '31.12.2010'
group by salesmanID) as s inner join salesman sm on s.salesmanID=sm.salesmanID