不能將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