PostgreSQL
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> PostgreSQL

Postgresql Nhiều số lượng cho một bảng

bạn có thể sử dụng case trong trường hợp này -

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

Nó sẽ giải quyết vấn đề của bạn

@S T Mohammed, Để có được kiểu như vậy, chúng ta chỉ cần sử dụng using sau group hoặc where điều kiện trong truy vấn bên ngoài, như bên dưới -

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Chuyển các biến C vào lệnh SQL

  2. Làm cách nào để sửa đổi hoặc xóa một đối tượng JSON cụ thể khỏi mảng JSON được lưu trữ trong kiểu cột jsonb trong PostgreSQL bằng cách sử dụng mệnh đề where?

  3. Truy vấn PostgreSQL để trả về kết quả dưới dạng danh sách được phân tách bằng dấu phẩy

  4. Postgres Lỗi phương pháp org.postgresql.jdbc.PgConnection.createClob () không được triển khai

  5. Cách hiểu một PHÂN TÍCH GIẢI THÍCH