获得游戏数量并显示玩过一半游戏的玩家

时间:2018-05-30 21:53:00

标签: sql sql-server

我在这些表中需要帮助,我希望SQL语句给我这个:

玩过半数或更多游戏的玩家可以假设我们在游戏类型中有6种不同的游戏。 table ...所以我想显示所有玩过3场或更多场比赛的球员。

喜欢:

ID     surName     number of games played
1      test1               3
2      test2               4
3      test3               3
4      test4               6

这就是我现在所做的事情:

SELECT        dbo.Player.ID, dbo.Player.surName, count(DISTINCT 
dbo.PlayerInAGame.gameTypeName) AS games_played, count(DISTINCT 
dbo.Game.gameTypeName) AS games_count
FROM            dbo.Player INNER JOIN
                     dbo.PlayerInAGame ON dbo.Player.ID = 
dbo.PlayerInAGame.playerID INNER JOIN
dbo.Game ON dbo.PlayerInAGame.gameTypeName = dbo.Game.gameTypeName AND 
dbo.PlayerInAGame.gameDateTime = dbo.Game.gameStartDateTime
GROUP BY dbo.Player.ID, dbo.Player.surName, dbo.Game.DealerInGame 

这是表格:

 create table Dealer
( 
number int identity(1000,1) primary key,
firstName nvarchar(20) not null,
surName nvarchar(20) not null,
birthDate date not null,
startWorkingDate date not null,
ID char(9) check(ID like replicate('[0-9]',9)) not null unique, 
check(datediff(year,birthDate,startWorkingDate)>=24) 
) 

create table GameType
( 
name nvarchar(20) primary key,
description nvarchar(20) not null,
minimumPlayers tinyint check (minimumPlayers > 0) not null,
maximumPlayers tinyint check (maximumPlayers > 0) not null, 
check(minimumPlayers <= maximumPlayers)
) 

create table Game
( 
gameTypeName nvarchar(20) references GameType(name) on delete cascade,
gameStartDateTime datetime,
gameEndTime time,
DealerInGame int not null references Dealer(number),
primary key(gameTypeName,gameStartDateTime) 
) 

create table Player
( 
ID char(9) primary key,
firstName nvarchar(20) not null,
surName nvarchar(20) not null,
city nvarchar(20) not null,
birthDate date check(datediff(year,birthDate,getdate())>=18) not null, 
preferred nvarchar(20) references GameType(name) on delete set null
) 

create table PlayerInAGame
( 
playerID char(9) references Player(ID),
gameTypeName nvarchar(20),
gameDateTime datetime,
betAmount int check(betAmount>0) not null,
winLosAmount int,
check((winLosAmount = -betAmount) or (winLosAmount>=betAmount)),
foreign key(gameTypeName,gameDateTime) references 
Game(gameTypeName,gameStartDateTime), primary 
key(playerID,gameTypeName,gameDateTime) ) 

create table PlayerDealerRelation
(
dealerNumber int references Dealer(number) on delete cascade,
playerID char(9) references Player(ID),
relationType char(1) check(relationType in ('P','G','B','C','U','N')), 
primary key(dealerNumber,playerID) 
) 

1 个答案:

答案 0 :(得分:1)

如果问题中的查询为您提供了正确的计数,那么添加额外的过滤器非常容易,只会留下games_played超过games_count一半的行。

只需添加HAVING

SELECT        
    dbo.Player.ID, 
    dbo.Player.surName, 
    count(DISTINCT dbo.PlayerInAGame.gameTypeName) AS games_played,
    count(DISTINCT dbo.Game.gameTypeName) AS games_count
FROM            
    dbo.Player 
    INNER JOIN dbo.PlayerInAGame ON dbo.Player.ID = dbo.PlayerInAGame.playerID 
    INNER JOIN dbo.Game 
        ON dbo.PlayerInAGame.gameTypeName = dbo.Game.gameTypeName 
        AND dbo.PlayerInAGame.gameDateTime = dbo.Game.gameStartDateTime
GROUP BY dbo.Player.ID, dbo.Player.surName, dbo.Game.DealerInGame 
HAVING 
    count(DISTINCT dbo.PlayerInAGame.gameTypeName) > 
    count(DISTINCT dbo.Game.gameTypeName) / 2