Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何使用子查询计算组中特定行的数量?_Sql Server_Group By_Count_Nested Query - Fatal编程技术网

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是的。但是我想用嵌套查询完成它