0

I am working on a table in a MySQL database which has 3 columns which are following,

event_name campaign_name customer_id
event_1    campaign_1    1
event_2    campaign_1    1
event_1    campaign_1    1
event_2    campaign_1    1
event_2    campaign_1    2

I want to query unique and total count of customer_id for each combination of event_name and campaign_name in the following format,

campaign_name    event_1_unique_count   event_1_total_count  event_2_unique_count   event_2_total_count
campaign_1       1                     2                   2                        2
       

Number of event names can be different that's why i am looking for a generic solution. Thanks.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
abhijeet
  • 849
  • 2
  • 19
  • 54
  • This is basically a pivot table. Have a look at this SO question and its most upvoted answer https://stackoverflow.com/questions/7674786/how-can-i-return-pivot-table-output-in-mysql another way, which I sometimes perform for the sake of speed, is export the resultset in Excel (or similar) and do the pivot there. – Bjoern Jun 11 '23 at 09:54
  • Search for dynamic pivot, you'll find plenty of questions / answers eg [this one](https://stackoverflow.com/questions/50596213/dynamic-pivot-table-columns-mysql) – Stu Jun 11 '23 at 09:56
  • 1
    thanks @Bjoern for guiding me towards pivot concept in mysql. – abhijeet Jun 11 '23 at 10:00
  • thanks @Stu for guiding me. – abhijeet Jun 11 '23 at 10:01

0 Answers0