MariaDBでテーブルをグループ化してソートする方法 - GROUP BY句とORDER BY句を組み合わせる
MariaDBでテーブルをグループ化してソートする
GROUP BY句は、SELECT句で指定した列に基づいて結果をグループ化します。各グループには、グループ内の行の数を示すCOUNT(*)
などの集計関数を適用できます。
ORDER BY句は、グループ内の行をソートする列を指定します。
以下に、MariaDBでテーブルをグループ化してソートする基本的な構文を示します。
SELECT 列名1, 列名2, ...
FROM テーブル名
GROUP BY グループ化列
ORDER BY ソート列;
例
次の例では、customers
テーブルをcountry
列でグループ化し、各グループ内の顧客数をカウントして、country
列で昇順にソートします。
SELECT country, COUNT(*) AS customer_count
FROM customers
GROUP BY country
ORDER BY country;
このクエリは次の結果を返します。
country | customer_count
------- | --------------
US | 100
JP | 50
CN | 20
詳細
- GROUP BY句で指定できる列は複数あります。複数の列でグループ化するには、カンマで区切って列名を列挙します。
SELECT country, city, COUNT(*) AS customer_count
FROM customers
GROUP BY country, city
ORDER BY country, city;
- ORDER BY句で指定できる列も複数あります。複数の列でソートするには、カンマで区切って列名を列挙します。ソート順序を指定するには、列名の後に
ASC
(昇順)またはDESC
(降順)を指定します。
SELECT country, city, COUNT(*) AS customer_count
FROM customers
GROUP BY country, city
ORDER BY country ASC, city DESC;
HAVING句
を使用して、グループ化された行をさらにフィルタリングできます。
SELECT country, city, COUNT(*) AS customer_count
FROM customers
GROUP BY country, city
HAVING customer_count > 10
ORDER BY country, city;
その他のヒント
MariaDBでテーブルをグループ化してソートするサンプルコード
SELECT country, COUNT(*) AS customer_count
FROM customers
GROUP BY country
ORDER BY country ASC;
例2:顧客テーブルを国と都市別にグループ化し、各都市の顧客数をカウントして、国と都市別に昇順にソートする
SELECT country, city, COUNT(*) AS customer_count
FROM customers
GROUP BY country, city
ORDER BY country ASC, city DESC;
SELECT country, city, COUNT(*) AS customer_count
FROM customers
GROUP BY country, city
HAVING customer_count > 10
ORDER BY country ASC, city DESC;
例4:顧客テーブルを国別にグループ化し、各国の顧客数の合計と平均を計算する
SELECT country, SUM(customer_count) AS total_customers, AVG(customer_count) AS avg_customers
FROM customers
GROUP BY country
ORDER BY country ASC;
SELECT country, city, MAX(customer_count) AS max_customers, MIN(customer_count) AS min_customers
FROM customers
GROUP BY country, city
ORDER BY country ASC, city DESC;
SELECT country, COUNT(*) AS customer_count,
(SELECT city
FROM customers
WHERE country = c.country
ORDER BY customer_count DESC
LIMIT 1) AS max_customer_city
FROM customers c
GROUP BY country
ORDER BY country ASC;
これらのサンプルコードは、MariaDBでテーブルをグループ化してソートする基本的な方法を示しています。ご自身のニーズに合わせて、これらのコードをカスタマイズすることができます。
MariaDBでテーブルをグループ化してソートするその他の方法
MariaDB 10.2以降では、ウィンドウ関数を使用して、テーブルをグループ化してソートすることができます。ウィンドウ関数は、特定の行範囲にわたって集計値を計算する関数です。
SELECT country,
COUNT(*) OVER (PARTITION BY country) AS customer_count
FROM customers
ORDER BY country ASC;
SELECT country, city,
COUNT(*) OVER (PARTITION BY country, city) AS customer_count
FROM customers
ORDER BY country ASC, city DESC;
SELECT country, city,
MAX(customer_count) OVER (PARTITION BY country, city) AS max_customers,
MIN(customer_count) OVER (PARTITION BY country, city) AS min_customers
FROM customers
ORDER BY country ASC, city DESC;
サブクエリを使用して、テーブルをグループ化してソートすることもできます。サブクエリは、別のクエリを実行して結果を返すクエリです。
SELECT country, (SELECT COUNT(*) FROM customers c2 WHERE c2.country = c.country) AS customer_count
FROM customers c
ORDER BY country ASC;
SELECT country, city, (SELECT COUNT(*) FROM customers c2 WHERE c2.country = c.country AND c2.city = c.city) AS customer_count
FROM customers c
ORDER BY country ASC, city DESC;
SELECT country, city,
(SELECT MAX(customer_count) FROM customers c2 WHERE c2.country = c.country AND c2.city = c.city) AS max_customers,
(SELECT MIN(customer_count) FROM customers c2 WHERE c2.country = c.country AND c2.city = c.city) AS min_customers
FROM customers c
ORDER BY country ASC, city DESC;
これらの方法の比較
- ウィンドウ関数は、サブクエリよりも簡潔で効率的な場合があります。
mariadb