不能将COUNT(一个聚合函数)与一个不相关的表一起使用,即表[SOMETABLE]和[SomeOtherTable]之间必须存在某种关系。
一个可能的方法来完成这项工作将是:SQL code here
如果您不能访问该链接,这里是架构代码:
create table yourtable (id int, graduates int, group_code varchar(100));
insert into yourtable values
(1, 50, 'others'),(1, 20, 'something'), (2, 20, 'total'), (3, 35, 'total2'),
(4, 40, 'total3');
create table othertable (ids int, graduate int, class varchar(100))
insert into othertable values (3,2, 'others')
修改后的声明:
SELECT SUM(CASE WHEN (o.ids) = 3 THEN 1 ELSE 0 END)
FROM yourtable as y inner join othertable as o on y.group_code = o.class
where o.graduate = 2
**感谢Giles,我将COUNT聚合函数改为SUM
我没有看到anythi这个问题不好。 – user1940676