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

Postgresql1つのテーブルの複数のカウント

    caseを使用できます この場合-

    SELECT type, 
           sum(case when place  = 'home' then 1 else 0 end) as Home,
           sum(case when  place  = 'school' then 1 else 0 end) as school,
           sum(case when  place  = 'work' then 1 else 0 end) as work,
           sum(case when  place  = 'cafe' then 1 else 0 end) as cafe,
           sum(case when  place  = 'friends' then 1 else 0 end) as friends,
           sum(case when  place  = 'mall' then 1 else 0 end) as mall
      from reports
     group by type
    

    それはあなたの問題を解決するはずです

    @S T Mohammed、このようなタイプを取得するには、usingを使用するだけです。 groupの後 またはwhere 以下のように、外部クエリの条件-

    select type, Home, school, work, cafe, friends, mall from (
    SELECT type, 
           sum(case when place  = 'home' then 1 else 0 end) as Home,
           sum(case when  place  = 'school' then 1 else 0 end) as school,
           sum(case when  place  = 'work' then 1 else 0 end) as work,
           sum(case when  place  = 'cafe' then 1 else 0 end) as cafe,
           sum(case when  place  = 'friends' then 1 else 0 end) as friends,
           sum(case when  place  = 'mall' then 1 else 0 end) as mall
      from reports
     group by type
     )
     where home >0 and School >0 and Work >0 and cafe>0 and friends>0 and mall>0
    


    1. MySQL追跡システム

    2. エラーORA-01804のテキストを取得しようとしたときにエラーが発生しました

    3. OracleDBから画像を取得する

    4. mysqlフィールドが完了した場合にリダイレクトするPHPコード