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?

PostgreSQLでFILTERを使った件数取得

0
Last updated at Posted at 2026-03-27

はじめ

PostgresSQでフラグごとの件数を1行で取得したいケースがあったのでFILTEを用いた書き方を残しておく。

前提

PostgreSQL 9.4以上

ステータスが「1」のと「2」の件数を1行で取得する場合

sample.sql
SELECT
    -- 条件1: statusが'1'のものをカウント
    COUNT(*) FILTER (WHERE status = '1') AS s1_count,
    
    -- 条件2: statusが'2'のものをカウント
    COUNT(*) FILTER (WHERE status = '2') AS s2_count,
FROM
    table;
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?