Alex.Six
Alex.Six

Reputation: 37

Count all occurences in a table with mulptiple columns (SQL)

I have a table that has partner id followed by multiple column combinations. My task is to count how many partners, each of those combinations have.

Here is an example of a table I'm working with:

enter image description here

And here is what I am looking to do:

enter image description here

Does anyone knows how to sort this out?

thanks

Upvotes: 0

Views: 35

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562270

SELECT 
  MAX(COMB_1) AS COMB_1,
  MAX(COMB_2) AS COMB_2,
  MAX(COMB_3) AS COMB_3,
  MAX(CATEGORY) AS CATEGORY,
  COUNT(*) AS PARTNER_COUNT
FROM NoOneNamesTheirTableInSQLQuestions
GROUP BY CONCAT_WS(';', COMB_1, COMB_2, COMB_3);

This uses a MySQL function CONCAT_WS(), but there should be equivalent ways of doing this in each of the other databases you tagged your question: Oracle and SQLite.

Upvotes: 1

Related Questions