0
    SUM (CASE
                WHEN T6.Currency =
(
    SELECT A0.MainCurncy
    FROM '+@myTempTableName+'.dbo.OADM A0
)
                THEN T6.LineTotal
              else T6.TotalFrgn
            END) as [Mf.Amount],

I got this error:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Can anyone help me?

rjose
  • 557
  • 5
  • 13
  • Possible duplicate of [SQL Server "cannot perform an aggregate function on an expression containing an aggregate or a subquery", but Sybase can](https://stackoverflow.com/questions/15751241/sql-server-cannot-perform-an-aggregate-function-on-an-expression-containing-an) – DrHouseofSQL May 17 '18 at 11:34
  • We need to see the rest of the query. What is `T6` ? – Tim Biegeleisen May 17 '18 at 11:34

1 Answers1

0

You need to fix the overall query:

SELECT . . .
    SUM(CASE WHEN A0.MainCurncy IS NOT NULL
             THEN T6.LineTotal
             ELSE T6.TotalFrgn
         END) as [Mf.Amount],

FROM . . . LEFT JOIN
     @myTempTableName+'.dbo.OADM A0
     ON A0.MainCurncy = T6.Currency
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786