Home Forums SQL Server 2008 T-SQL (SS2K8) Error: Each GROUP BY expression must contain at least one column that is not an outer reference. RE: Error: Each GROUP BY expression must contain at least one column that is not an outer reference.

  • Doug,

    Thank you for your reply. The SQL compiler was not happy with the join clause movement that you had proposed. Nonetheless, your suggestion got me to thinking about a new approach to this query. The result is shown below. The result is quite a bit more compact and, to my eyes at least, a good bit more readable than my first version. The query below runs and it generates the correct results when tested on a modest size data set. Using "Sum(Distinct...)" on columns I'm not actually summing to avoid "Group By" protestations seems a bit tacky, but it works, and as far as I can see, should never cause any problems. Anyhow, apart from adding a little further conditioning to the inner where clause, I believe this will do it for me.

    ***Actually, check that. Just before hitting post I took one last look at the query and believe I have an error both in it and my testing methodology. I'll have to take a closer look tomorrow...

    Thanks again -

    UPDATEMDEP

    SetMDEP.POObligations =

    (

    Select (SUM(PO_DX.QuantityOrdered * PO_DX.Price)- SUM(Distinct PO_MX.PaymentPosted)) * SUM(Distinct(cast(PO_MX.PrimarySplit as decimal(5,2))/100))

    from PO_MX

    inner join PO_DX on PO_DX.PONumber = PO_MX.PONumber

    Where PO_MX.MDEP = MDEP.MDEP

    )

    WHEREMDEP.MDEP IN

    (Select MDEP From PO_MX)