mattdh12
mattdh12

Reputation: 57

SQL - Selecting All Except

I have a table of NBA player stats of all NBA players from January 1 to January 7. The data in the table is set up exactly like it is here: http://www.basketball-reference.com/friv/dailyleaders.cgi?month=1&day=1&year=2014

I'm trying to run a query to return the stats for when a list of players from different teams do not play due to injury. When a player does not play, he is not listed in the player column for that date. Right now I have a query that returns the stats for when players DO play:

select
    t2.player,
    count(t2.player) as Game_Count,
    t2.tm,
    round(avg(t2.minutes), 2) as Min
from
    nba.player_stats t1
        inner join
    nba.player_stats t2 ON t1.date = t2.date and t1.tm = t2.tm
where
    t1.player = 'Courtney Lee'
        or t1.player = 'Ryan Anderson'
group by player;

This returns enter image description here

There are three listed teams here because Courtney Lee was traded from BOS to MEM between the 1-01-14 and 1-07-14.

I'm trying to return all data except for the selected. This includes all other teams (ATL, BRK, etc.) I was thinking just saying "player<>" instead of "player=" and using distinct would work, but doing that produces some results that don't make sense to me (game count should be no more than 5): enter image description here

Upvotes: 1

Views: 522

Answers (1)

Scott
Scott

Reputation: 3732

To make sure I understand the question - you're looking for all player stats for those teams in which the selected players did NOT play in that year?

I'm going to suggest using NOT IN and a nested query here. This is a bit simpler. In addition, use the IN statement to list the players instead of multiple conditions.

select
    t.player,
    count(t.player) as Game_Count,
    t.tm,
    round(avg(t.minutes), 2) as Min
from
    nba.player_stats t
where
    t.tm not in (
                 select tm from nba.player_stats p
                 where  p.player in ('Courtney Lee','Ryan Anderson')
                        and p.date = t.date)
group by player;

Upvotes: 1

Related Questions