sql >> データベース >  >> RDS >> Mysql

ゴルフのスコアを保存するためのスケーラブルなデータベーススキーマを作成する

    私の平均、最高、最低のスコアは何でしたか?

    シナリオ1。

    select (h1+h2+h3+h4+h5+h6+h7+h8+h9+h10+h11+h13+h14+h15+h16+h17+h18) / 18 as avg_score
          ,greatest(h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) as highest
          ,least(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) as lowest
      from scores
     where player_id = 1;
    

    select avg(score) as avg_score
          ,max(score) as highest
          ,min(score) as lowest
      from scores
     where player_id = 1;
    

    私の最悪の穴はどれですか?

    シナリオ2。

    select case when h1 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H1'
                when h2 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H2'
                when h3 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H3'
                when h4 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H4'
                when h5 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H5'
                when h6 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H6'
                when h7 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H7'
                when h8 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H8'
                when h9 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H9'
                when h10 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H10'
                when h11 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H11'
                when h12 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H12'
                when h13 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H13'
                when h14 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H14'
                when h15 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H15'
                when h16 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H16'
                when h17 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H17'
                when h18 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H18'
            end as hole_highest_score      
      from scores
     where player_id = 1;
    

    select hole, score
      from scores s1
     where player_id = 1
       and score = (select max(score)
                      from scores s2
                     where s2.player_id = s1.player_id)
    

    私はいつでもシナリオ2に行きます:)



    1. 特定の日付範囲を取得する

    2. SQL Server Integration Services(SSIS)を使用してQuickBooksレコードにデータを入力する

    3. インデックスチューニングへのアプローチ–パート2

    4. SQL Server(T-SQL)で主キーの名前を変更する