Finding the Percentage

  • Thanks for this question about the fundamentals! 

    So something like this would work?
    create table #Batting
    (
    pk int identity(1,1) not null primary key,
    player varchar(25),
    batting_year int,
    hr_total int

    )

    insert into #Batting ( player, batting_year, hr_total )
    values ( 'Betts',2015,32),
    ( 'Betts',2016,40),
    ( 'Betts',2017,44),
    ( 'Betts',2018,29),
    ( 'Yelich',2015,26),
    ( 'Yelich',2016,33),
    ( 'Yelich',2017,31),
    ( 'Yelich',2018,47),
    ( 'Altuve',2015,39),
    ( 'Altuve',2016,29),
    ( 'Altuve',2017,18),
    ( 'Altuve',2018,24),
    ( 'Turner',2015,17),
    ( 'Turner',2016,23),
    ( 'Turner',2017,27),
    ( 'Turner',2018,41)

    select * from #Batting;

    select count(case when hr_total >= 40 then 1  end)  / cast(count(*) as decimal(6,2)) * 100
    from #Batting as b;

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Really nice question, thanks, Steve

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • @Solomon
    Thanks for the clear explanations, there are so many things you can get wrong even in something that simple!

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply