0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】ウィンドウ関数

0
Posted at

概要

カラム内を特定カテゴリーを使って、別のカラムのランキングを作る。

事前登録データ

生徒名 教科名 点数
生徒1 数学 80
生徒2 数学 90
生徒1 英語 85
生徒2 英語 70
生徒1 国語 89
生徒2 国語 90

SQL

SELECT
    "生徒名"
    , "教科名"
    , "点数" 
FROM
    ( 
        SELECT
            *
            , ROW_NUMBER() OVER (PARTITION BY "教科名" ORDER BY "点数" DESC) rank 
        FROM
            test_result 
        WHERE
            "教科名" IN ('英語', '数学')
    ) sq 
WHERE
    rank = 1
SELECT
    "生徒名"
    , "教科名"
    , "点数" 
FROM
    ( 
        SELECT
            *
            , DENSE_RANK() OVER (PARTITION BY "教科名" ORDER BY "点数" DESC) rank 
        FROM
            test_result 
        WHERE
            "教科名" IN ('英語', '数学')
    ) sq 
WHERE
    rank = 1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?