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

テーブルで選択し、いくつかの値の出現をカウントする方法

    すべて ケースは UNION を使用して簡単に実行できます ステートメント (sqlFiddle を参照) 結果):

    (SELECT v.country,
        v.site,
        SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations,
        SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations,
        SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations,
        SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined,
        COUNT(*) Total,
        0  'isAll'
    FROM violations v
    GROUP BY v.country, v.site)
    union(
    SELECT v.country,
        '- All -',
        SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations,
        SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations,
        SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations,
        SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined,
        COUNT(*) Total,
      1  'isAll'
    FROM violations v
    GROUP BY v.country)
    UNION (
    SELECT '- All -',
        '- All -',
        SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations,
        SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations,
        SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations,
        SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined,
        COUNT(*) Total,
      1  'isAll'
    FROM violations v)
    ORDER BY country, isAll DESC, site
    

    ただし、この種のクエリではパフォーマンスがあまり良くない可能性があるため、これが最善の解決策であるとは言えませんが、機能します。

    「わからない」を含むバージョン

    http://www.sqlfiddle.com/#!2/abfb7/21

    (SELECT IF(c.name IS NULL, '- Unknow -', c.name) as name,
        v.site,
        SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations,
        SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations,
        SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations,
        SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined,
        COUNT(*) Total,
        0  'isAll'
    FROM violations v LEFT JOIN country c ON c.name = v.country
    GROUP BY c.name, v.site)
    union(
    SELECT IF(c.name IS NULL, '- Unknow -', c.name) as name,
        '- All -',
        SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations,
        SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations,
        SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations,
        SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined,
        COUNT(*) Total,
      1  'isAll'
    FROM violations v LEFT JOIN country c ON c.name = v.country
    GROUP BY c.name)
    UNION (
    SELECT '- All -',
        '- All -',
        SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations,
        SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations,
        SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations,
        SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined,
        COUNT(*) Total,
      1  'isAll'
    FROM violations v LEFT JOIN country c ON c.name = v.country)
    ORDER BY name, isAll DESC, site
    


    1. MariaDBでのMAKEDATE()のしくみ

    2. mysqlデータベースへのôの挿入はRhôneの結果の一部ですRh

    3. 制約付きの列を削除する方法は?

    4. SQLサーバー上のリンクテーブルに追加するときにAccessが間違ったIDを使用しないようにする