How do I avoid multiplied results when adding a subquery?

  • I've created a query that includes a subquery.  When I run the subquery alone, it gives me the desired results.  When I run the main query without the subquery, it also gives me the desired results but when ran together, the 'amount' totals for sales and cost are multiplied.    What am I doing wrong??!!

    select
       monthyear
       ,agentname
       ,sum(sales.amount)
       ,max(cost.amount)
    from sales
       left outer join (select 
                                  monthyear
                                 ,agentname
                                  ,sum(amount)
                                   from cost
                                    group by agentname, monthyear)  cost
                                          on monthyear.sales = monthyear.cost
                                          and sales.agentname = cost.agentname
    group by sales.agentname, monthyear

  • Nevermind all...if found my issue.   my "on" conditions were not specific enough in the actual code.    once i fixed that, it worked like a charm!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply