5 Replies Latest reply on Apr 15, 2019 7:18 AM by Ewald Hofman

    Subquery: count of a count

    Laura Palacio

      Hello,

       

      I'm quite new to Tableau and I wanted to know if this is possible to do.

       

      I have two tables: one with the user information and another one with the purchase information. Every user can purchase the product as many times as they want to. I want to count the number of people that have purchased the product X number of times. So, in SQL, I need to use a subquery which is:

      select purchases, count(purchases) from (select count(distinct p.id) as purchases from account_purchase p left join account_customuser u on u.id = p.user_id group by u.id) as foo group by purchases order by purchases;

       

      The result it gives is:

      purchases | count

      -----------+-------

                    1 |  1263

                    2 |   429

                    3 |   16

                    4 |    8

                    5 |    2

                    6 |    1

                    7 |     2

                    8 |     3

                    9 |    3

       

      However, in Tableau Desktop, I cannot find a way to do that. I have been able to calculate the subquery and show the number of purchases each user has made. But then, I would like to take this data and group by the number of purchases in order to determine the number of people that have bought the product 2 times, 3 times, etc.

       

      Is there a way to do that?

       

      Thank you very much for your help.