Skip to content
Advertisement

Postgres – Unique values for id column using CTE, Joins alongside GROUP BY

I have a table referrals:

And another table activations

I am trying to generate another table from the two tables that has only unique values for referrals.id and returns as one of the columns the count for each apps as best_selling_app_count.

Here is the query I ran:

Here is the result I got:

The problem with this result is that the table has a duplicate id of 2. I only need unique values for the id column.

I tried a workaround by harnessing distinct that gave desired result but I fear the query results may not be reliable and consistent. Here is the workaround query:

I need a recommendation on how best to achieve this.

Advertisement

Answer

I am trying to generate another table from the two tables that has only unique values for referrals.id and returns as one of the columns the count for each apps as best_selling_app_count.

Your question is really complicated with a very complicated SQL query. However, the above is what looks like the actual question. If so, you can use:

You have not explained the other columns that are in your result set.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement