Sql server 如何使用子查询计算组中特定行的数量?
模式如下:Sql server 如何使用子查询计算组中特定行的数量?,sql-server,group-by,count,nested-query,Sql Server,Group By,Count,Nested Query,模式如下: create table Game ( gameID int not null, [name] varchar(30) not null, developID int, cost int not null, category varchar(30) not null, unique (gameID), primary key(gameID), ); create table Developer ( developer
create table Game
(
gameID int not null,
[name] varchar(30) not null,
developID int,
cost int not null,
category varchar(30) not null,
unique (gameID),
primary key(gameID),
);
create table Developer
(
developerID int not null,
[name] varchar(30) not null,
country varchar(30) not null,
speciality varchar(30) not null,
unique (developerID),
primary key (developerID)
);
现在,我想知道一个美国人为每个类别开发的游戏数量如果没有美国人开发过该特定类别的游戏,则必须显示0
如何为该需求编写嵌套SQL查询。
谢谢。我相信这会满足您的需要。除非有什么原因需要嵌套查询,否则我认为这会起作用
select count(*), category
from Game g
join Developer d on g.developID = d.developerID
where country = 'USA'
group by category
在嵌套查询格式中,这应该可以工作,但它的效率低于联接版本:
select count(*)
from Game g
where developID in (select developerid from Developer where country = 'USA')
Game.developepid=Developer.developerID吗?@jw11432是的。但是我想用嵌套查询完成它