Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get count of games and display the players who played half of games

Tags:

sql

sql-server

i Need help in these tables, i want SQL statement to give me this :

the players who played in half or more of the games, lets assume that we have 6 different games in 'GameType' table ... So I want to display all players who played 3 or more games.

like :

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

this what i done until now:

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 

here is the tables:

 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) 
) 
like image 380
Rabeea qabaha Avatar asked Feb 04 '26 06:02

Rabeea qabaha


1 Answers

If your query in the question gives you the correct count, then it is very easy to add an extra filter that would leave only those rows where games_played is more than half of games_count.

Just add 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
like image 93
Vladimir Baranov Avatar answered Feb 05 '26 19:02

Vladimir Baranov