How to count many-to-many relations in postgresql

Timur Malin

I have a table that looks like:

 a | b
---+---
 1 | a
 2 | a
 3 | a

 1 | b
 3 | b

 2 | c
 3 | c

It represents many-to-many relatons a<->b. I'd like to get all existing relations a<->count(b)<->a, like:

 a1 | a2 | count
----+----+-------
 1  |  2 |     1         #1<->a<->2
 1  |  3 |     2         #1<->(a,b)<->3

 2  |  1 |     1         #duplicate for 1<->a<->2
 2  |  3 |     2         #2<->(a,c)<->3

 3  |  1 |     2         #duplicate for 1<->(a,b)<->3 
 3  |  1 |     2         #duplicate for 2<->(a,c)<->3

I've managed it yet for single a, but can't figure out how to cycle through all:

SELECT
 '1' AS a1,
 t1.a AS a2,COUNT(t1.b)
FROM 
 a_b t1
INNER JOIN(
  SELECT
   b
  FROM a_b
  WHERE
   a = '1'
  ) t2
ON
 t1.b = t2.b
WHERE t1.a != '1'
GROUP BY t1.a
ORDER BY t1.a;

 a1 | a2 | count
----+----+-------
 1  |  2 |     1
 1  |  3 |     2

Is it achievable without cross joining a_b on itself or looping through external script?

Here is SQLFiddle http://www.sqlfiddle.com/#!1/8b53a/1/0

TIA

sekky

A little addition to Gordon Linoff's solution: In order to not get the relations double (i.e. 1-3 and 3-1) I added the where clause:

select ab1.a as a1, ab2.a as a2, count(*)
from a_b ab1 join
     a_b ab2
     on ab1.b = ab2.b and ab1.a <> ab2.a
     where ab1.a < ab2.a
group by ab1.a, ab2.a

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

mysql query - one to many relations

분류에서Dev

Count and display how many of specific column result

분류에서Dev

Count how many low and high guesses

분류에서Dev

SQL many to many relation: create N random records in relations table

분류에서Dev

SQL many-to-many count sum

분류에서Dev

Elegant way to count how many pieces of each type are present in a position

분류에서Dev

Problem with fetching data from database in one to many polymorphic relations in Laravel

분류에서Dev

MS SQL Server - storing many-to-many relations between several tables

분류에서Dev

Filtering many-to-many count expression using existing subquery

분류에서Dev

Count many to many relationship items Doctrine, Symfony2

분류에서Dev

How to operate on the table many to many EF

분류에서Dev

How to do JavaScript to Count how many remaining words are on a combination of 4 textarea fields

분류에서Dev

How can I make a Many to many relation on same table (many to many Join To Self)

분류에서Dev

Count how many seconds did the user hover an element using jquery or javascript?

분류에서Dev

How to build object with many relationship

분류에서Dev

How create many list as array?

분류에서Dev

how to pass many arguments to for loop?

분류에서Dev

How many packages are in the main repository?

분류에서Dev

Rails has_and_belongs_to_many relations with two types of Users and one type of Table

분류에서Dev

How to add many-to-many relationships through a form?

분류에서Dev

Using Ember Data's One to Many or Many to Many, how do you specify the relationship

분류에서Dev

doctrine 2 query builder count one to many relationship

분류에서Dev

Postgresql의 <MANY>에서 <ONE> 업데이트

분류에서Dev

How to calculate how many rows and columns are needed

분류에서Dev

How to calculate into how many parts network will be divided

분류에서Dev

How to find how many paragraphs in a file?

분류에서Dev

Django query filter many to many to many etc

분류에서Dev

How to create many graphs on chart in Google sheets?

분류에서Dev

how to read xml file with many nodes with javascript

Related 관련 기사

  1. 1

    mysql query - one to many relations

  2. 2

    Count and display how many of specific column result

  3. 3

    Count how many low and high guesses

  4. 4

    SQL many to many relation: create N random records in relations table

  5. 5

    SQL many-to-many count sum

  6. 6

    Elegant way to count how many pieces of each type are present in a position

  7. 7

    Problem with fetching data from database in one to many polymorphic relations in Laravel

  8. 8

    MS SQL Server - storing many-to-many relations between several tables

  9. 9

    Filtering many-to-many count expression using existing subquery

  10. 10

    Count many to many relationship items Doctrine, Symfony2

  11. 11

    How to operate on the table many to many EF

  12. 12

    How to do JavaScript to Count how many remaining words are on a combination of 4 textarea fields

  13. 13

    How can I make a Many to many relation on same table (many to many Join To Self)

  14. 14

    Count how many seconds did the user hover an element using jquery or javascript?

  15. 15

    How to build object with many relationship

  16. 16

    How create many list as array?

  17. 17

    how to pass many arguments to for loop?

  18. 18

    How many packages are in the main repository?

  19. 19

    Rails has_and_belongs_to_many relations with two types of Users and one type of Table

  20. 20

    How to add many-to-many relationships through a form?

  21. 21

    Using Ember Data's One to Many or Many to Many, how do you specify the relationship

  22. 22

    doctrine 2 query builder count one to many relationship

  23. 23

    Postgresql의 <MANY>에서 <ONE> 업데이트

  24. 24

    How to calculate how many rows and columns are needed

  25. 25

    How to calculate into how many parts network will be divided

  26. 26

    How to find how many paragraphs in a file?

  27. 27

    Django query filter many to many to many etc

  28. 28

    How to create many graphs on chart in Google sheets?

  29. 29

    how to read xml file with many nodes with javascript

뜨겁다태그

보관