Skip to content
Advertisement

Duplicates in pre-aggregated sub-query sql

I have two tables with many-to-many relationship. I am trying to get values from both of the table where UserId is unique (I’m joining these table on this value)

I am rying to use pre aggregated query, but I get error

Column 'clv.ProbabilityAlive' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I understand that I should add these all values to group by clause, but then I am getting duplicates because peakClv values repeat.

If i am using simple join then it takes forever because of many to many relationship.

this is my query:

I am a bit out of ideas could someone lend a hand?

I also tried using distinct like one answer suggested:

but I still get duplicates: enter image description here

Advertisement

Answer

If you have not aggregation function like SUM(), MAX() .. you can’t use GROUP BY

if you need distinct (not repeated rows) use distinct

but looking to you img seems you need an aggregation function on PeakClv eg max() and group by

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