I need help! I get an aggregate error with my SQL Query

Hi Experts,
I need your help with my query.  I get the following aggregate error with the below SQL Query

Error I get:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

My Current query:
SELECT  S.SKU, S.ALTSKU AS UPC, 
     CASE WHEN IC.Available < 1 THEN 
          CASE IC.Category WHEN 'Tops' THEN 
               CASE WHEN (ISNULL(FLOOR(SUM(SKX.QTY - SKX.QTYALLOCATED - 
               (SELECT SUM(ARTA.quantity) FROM OrdDetail AS ARTA LEFT JOIN OrdHeader AS ARTB ON ARTA.Ordernumber = ARTB.Ordernumber WHERE ARTA.itemnmbr = S.SKU)
               ) * IC.Available), 0)) < 15 THEN 0 ELSE 
               ISNULL(FLOOR(SUM(SKX.QTY - SKX.QTYALLOCATED - 
               (SELECT SUM(ARTA.quantity) FROM OrdDetail AS ARTA LEFT JOIN OrdHeader AS ARTB ON ARTA.Ordernumber = ARTB.Ordernumber WHERE ARTA.itemnmbr = S.SKU)
               ) * IC.Available), 0) 
               END
          END
     END AS SKU_QTY
FROM   Cur_Inventory AS R INNER JOIN
       Inventory_Cat AS IC ON R.SKU = IC.SKU INNER JOIN
       SKU AS S ON IC.SKU = S.SKU LEFT OUTER JOIN
       LOT AS SKX ON S.SKU = SKX.SKU
GROUP BY S.SKU, S.ALTSKU, IC.Category, IC.Available
ORDER BY S.SKU

Open in new window

mainrotorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Something like this perhaps:
SELECT  S.SKU,
        S.ALTSKU AS UPC,
        CASE WHEN IC.Available < 1
             THEN CASE IC.Category
                    WHEN 'Tops' THEN CASE WHEN (ISNULL(FLOOR(SUM(SKX.QTY - SKX.QTYALLOCATED - ISNULL(o.TotalQuantity, 0)) * IC.Available), 0)) < 15 THEN 0
                                          ELSE ISNULL(FLOOR(SUM(SKX.QTY - SKX.QTYALLOCATED - ISNULL(o.TotalQuantity, 0)) * IC.Available), 0)
                                     END
                  END
        END AS SKU_QTY
FROM    Cur_Inventory AS R
        INNER JOIN Inventory_Cat AS IC ON R.SKU = IC.SKU
        INNER JOIN SKU AS S ON IC.SKU = S.SKU
        LEFT OUTER JOIN LOT AS SKX ON S.SKU = SKX.SKU
        LEFT JOIN (
                   SELECT   ARTA.itemnmbr,
                            SUM(ARTA.quantity) TotalQuantity
                   FROM     OrdDetail AS ARTA
                            LEFT JOIN OrdHeader AS ARTB ON ARTA.Ordernumber = ARTB.Ordernumber
                   GROUP BY ARTA.itemnmbr
                  ) o ON o.itemnmbr = S.SKU
GROUP BY S.SKU,
        S.ALTSKU,
        IC.Category,
        IC.Available
ORDER BY S.SKU

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
It's a little difficult to tell what you're trying to accomplish but, typically, using case with aggregrate functions would be more like -
sum(case when ... then something else someotherthing ...
rather than case when then sum(case when ...
Perhaps you can post some sample relevant data and your desired results to help clarify your intent.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.