简体   繁体   中英

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:

在此处输入图片说明

And here is what I am looking to do:

在此处输入图片说明

Does anyone knows how to sort this out?

thanks

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM