Postgresql query for fetching records

Dhairya Lakhera

In the below example i want to fetch the no of centers which has different subcenters. means for center 01 and 04 , subcenter are same. but for centers 05 and 07 , subcenters are different. so please help me writing a query that help me to fetch number of centers that have different subcenters.

  ----------------------
  | Center | Subcenter |
  ----------------------
  | 01     |  002      |
  | 01     |  002      |
  | 04     |  001      |
  | 04     |  001      |
  | 04     |  001      |
  | 05     |  001      |
  | 05     |  001      |
  | 05     |  002      |
  | 07     |  003      |
  | 07     |  003      |
  | 07     |  004      |
  | 07     |  005      |
  ----------------------

I want the result as below

  --------------------
  | count | center   |
  --------------------
  | 2     |  05      |
  | 3     |  07      | 
  --------------------

because 05 has 001 and 002 (two different subcenter) , and 07 has 003, 004 and 005 (three different subcenter)

davek
select center, count(distinct subcenter) as c
from x
group by center
having count(distinct subcenter) > 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysqli query fetching no records

From Dev

Fetching CloudKit Records By Name and Query in PHP

From Dev

Query Response for fetching receive payment records from quickbooks db?

From Dev

PostgreSQL how to count records in a recursive query

From Dev

Error in fetching records in angularjs

From Dev

Laravel : Fetching records with condition

From Dev

Fetching 1000 + records quikly

From Dev

DB2 Query for fetching records from first query, if first query fails, then fetch from 2nd query

From Dev

Query for fetching mismatching records from a table by cross checking with 2 other tables

From Dev

Fetching crossed records from database

From Dev

mysql reverse order of records fetching

From Dev

mysql reverse order of records fetching

From Dev

Fetching all records Through PDO

From Dev

Fetching Firebase Records Based on Email

From Dev

PDO error while fetching records

From Dev

Fetching records from Cust table

From Dev

Postgresql Iterate over an array field and use the records for another query

From Dev

How to query PostgreSQL for all records where hstore is empty?

From Dev

Returning the first X records in a postgresql query with a unique field

From Dev

Retrieve first N records of a JSON array with a Postgresql query

From Dev

postgresql query to determine recommendation records that haven't been seen by a user

From Dev

Rails app with postgresql; query with .take does not keep the order of records

From Dev

Retrieve first N records of a JSON array with a Postgresql query

From Dev

Postgresql query to select records that do not reference another member in the group

From Dev

How to query for a list of associated records using Rails 4 + PostgreSQL?

From Dev

Query in MongoDb for fetching record

From Dev

Issue with fetching query

From Dev

Rails 5.1 (postgresql): efficient DB query to select parent records and filtered children records

From Dev

fetching compact version of JSONB in PostgreSQL

Related Related

  1. 1

    Mysqli query fetching no records

  2. 2

    Fetching CloudKit Records By Name and Query in PHP

  3. 3

    Query Response for fetching receive payment records from quickbooks db?

  4. 4

    PostgreSQL how to count records in a recursive query

  5. 5

    Error in fetching records in angularjs

  6. 6

    Laravel : Fetching records with condition

  7. 7

    Fetching 1000 + records quikly

  8. 8

    DB2 Query for fetching records from first query, if first query fails, then fetch from 2nd query

  9. 9

    Query for fetching mismatching records from a table by cross checking with 2 other tables

  10. 10

    Fetching crossed records from database

  11. 11

    mysql reverse order of records fetching

  12. 12

    mysql reverse order of records fetching

  13. 13

    Fetching all records Through PDO

  14. 14

    Fetching Firebase Records Based on Email

  15. 15

    PDO error while fetching records

  16. 16

    Fetching records from Cust table

  17. 17

    Postgresql Iterate over an array field and use the records for another query

  18. 18

    How to query PostgreSQL for all records where hstore is empty?

  19. 19

    Returning the first X records in a postgresql query with a unique field

  20. 20

    Retrieve first N records of a JSON array with a Postgresql query

  21. 21

    postgresql query to determine recommendation records that haven't been seen by a user

  22. 22

    Rails app with postgresql; query with .take does not keep the order of records

  23. 23

    Retrieve first N records of a JSON array with a Postgresql query

  24. 24

    Postgresql query to select records that do not reference another member in the group

  25. 25

    How to query for a list of associated records using Rails 4 + PostgreSQL?

  26. 26

    Query in MongoDb for fetching record

  27. 27

    Issue with fetching query

  28. 28

    Rails 5.1 (postgresql): efficient DB query to select parent records and filtered children records

  29. 29

    fetching compact version of JSONB in PostgreSQL

HotTag

Archive