OCP here ya go:
The highlighted portions are the main drivers for this to take place.
In my case I had “Sale_Number” that could occur 2 or more times, so Max(sale_number) would give me one of the two records (this separates the two even though they are the same).
NOTE: using Min(sale_number) would also work of course.
Then, in the “inner select” statement just add in your criteria (FROM, WHERE, etc…)
The case statement at top is where it’s decided to set the value of the duplicate to 0 and the other to the original amount (or whatever you want them to be) based on the COUNT(*) from the inner query. You can add and extend what’s done with other duplicates within the case easily.
It's a nice little piece of code, hope it helps!Code:SELECT dis.name AS dist_id, pat.account_number, Substr(pat.Sale_number,1,4) as Sale_Num, pat.sale_date, case when inner.sale_count is null or inner.sale_count = 1 then revenue_dollar_amount else 0 end as revenue_outputFROM acc_tbl pat LEFT JOIN (SELECT Substr(pat.Sale_number,1,4) as Sale_Num, count(*) as sale_count, max(sale_number) as second_sale_number FROM pay_acc_tbl pat Where pat.stat_flag = 'P' GROUP BY substr(pat.sale_number,1,4)) inner ON inner.second_sale_number = pat.sale_number JOIN dist_tbl dis ON dis.id = pat.dist_id WHERE pat.stat_flag = 'A' ORDER by sale_num




Reply With Quote