SQL SUM Question Group By

Good afternoon,

I have a query where I do the following:
SELECT CustomerID,
              SUM(Cost) Cost,
              SUM(Quantity) Quantity,
              LongShort,
              SUM(Alpha) Alpha
FROM Customer
GROUP BY CustomerID, LongShort

What I need to do is if CustomerID = 1 then I want to make the LongShort return 'L' if the SUM(Quantity) >= 0 ELSE 'S' otherwise return the LongShort as it is.   How can I do this because I can't place the SUM in the Group by with a CASE statement?  I am trying to avoid doing nested queries etc. if possible.
sbornstein2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

zephyr_hex (Megan)DeveloperCommented:
You can wrap your query in another SELECT that returns the results you want:

SELECT CustomerID, Cost, Quantity,Alpha
,CASE WHEN CustomerID = 1 AND Quantity >= 0 THEN 'L' WHEN CustomerID = 1 THEN 'S' ELSE LongShort END AS LongShort
FROM (
SELECT CustomerID,
              SUM(Cost) Cost,
              SUM(Quantity) Quantity,
              LongShort,
              SUM(Alpha) Alpha
FROM Customer
GROUP BY CustomerID, LongShort
) u

Open in new window


You may need to aggregate again since you are grouping by LongShort:

SELECT u.CustomerID, SUM(u.Cost) AS Cost, SUM(u.Quantity) as Quantity,SUM(u.Alpha) AS Alpha
,CASE WHEN u.CustomerID = 1 AND u.Quantity >= 0 THEN 'L' WHEN u.CustomerID = 1 THEN 'S' ELSE u.LongShort END AS LongShort
FROM (
SELECT CustomerID,
              SUM(Cost) Cost,
              SUM(Quantity) Quantity,
              LongShort,
              SUM(Alpha) Alpha
FROM Customer
GROUP BY CustomerID, LongShort
) u
GROUP BY u.CustomerID, CASE WHEN u.CustomerID = 1 AND u.Quantity >= 0 THEN 'L' WHEN u.CustomerID = 1 THEN 'S' ELSE u.LongShort END

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
NerdsOfTechTechnology ScientistCommented:
You will need a subquery (with an alias) since a HAVING clause won't do the trick.

 SELECT 
    t2.CustomerID
  , t2.Cost
  , t2.Quantity
  , "LongShort" = 
    CASE 
     WHEN t2.CustomerID = 1 THEN "L"
     WHEN t2.Quantity >= 0 THEN "S"
     ELSE t1.LongShort
    END
  , t2.Alpha
 FROM Customer t1
 INNER JOIN
 (
  SELECT 
     CustomerID,
   , SUM(Cost) Cost,
   , SUM(Quantity) Quantity,
   , SUM(Alpha) Alpha
  FROM Customer
  GROUP BY CustomerID
 ) t2
 ON t1.CustomerID = t2.CustomerID

Open in new window

0
Vikas GargAssociate Principal EngineerCommented:
Hello,

You can try the below query


SELECT CustomerID,
              SUM(Cost) Cost,
              SUM(Quantity) Quantity,
              LongShort,
              SUM(Alpha) Alpha,
			  CASE WHEN CustomerID = 1 THEN
			  CASE WHEN SUM(Quantity) >=0 THEN 'L' ELSE 'S' END
			  ELSE LongShort END AS LongShortNew
FROM Customer
GROUP BY CustomerID, LongShort

Open in new window

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
SQL

From novice to tech pro — start learning today.