jeudi 21 avril 2016

SQL Error of Cannot perform an aggregate function on an expression containing an aggregate or a subquery

I was executing this query in SQL but is giving me an error like this "Cannot perform an aggregate function on an expression containing an aggregate or a sub query." Is there anyone who can help me with it. I am really stuck. Thank you Here is my query.

SELECT [id]
      ,[name]
      ,SUM(case 
           when [code] = 1  Then 
           case 
             when exists(select * from [sampletab] where [id] = [id]and [code] = 2) then

                case when exists(select * from [sampletab] where [id] = [id] and [code] = 4) then 100 
                else 100
                end 

               else 100
               end    
           when [code] = 8 then 200
           when code = 2 then 50
           when code = 4 then 20

      end
      ) as "totl"

  FROM [test].[dbo].[sampletab]
  GROUP BY id , name

My data

Aucun commentaire:

Enregistrer un commentaire