Home > Mobile >  Count the number of duplicate column pairs
Count the number of duplicate column pairs

Time:01-29

I am having difficulty with writing an expression for counting the number of duplicate column pairs. For example, I have a table with two columns:

BUYER SELLER
ALEX 1
ALEX 1
ALEX 1
ALEX 2
ALEX 2
JOE 1
JOE 3
JOE 3

I want to count the number of matching pairs for buyer and seller, and create a new column (count) based on the total number of matching pairs, as such:

BUYER SELLER COUNT
ALEX 1 3
ALEX 2 2
JOE 1 1
JOE 3 2

I know that the COUNT function is required to solve this, but am not sure how to implement it.

I would appreciate any help! Thanks.

CodePudding user response:

You want to GROUP BY the buyer and seller columns and then aggregate using the COUNT function:

SELECT buyer, seller, COUNT(*)
FROM   table_name
GROUP BY buyer, seller
  • Related