sum in 2 diffrent ways from the same table

hi there ,
this is my code :
ths code is working but i need to add more sum result
this sum res need to only for products the the Postive value is true
like that :

select ProRunId,ProMkt,ProName,ProMinInStock,ProRem,SupName,
 (select Sum (MoveDepQuant) - sum(MoveWithQuant)) as Bal

Here i need to add more Sum(MoveDepQuant) as Pos only for the products that have the
value where Postive=1


from dbo.Products
 inner join dbo.Moves on ProRunId=MoveProRunId inner join dbo.Supplier on Products.SupRunId=Supplier.SupRunId
 group by ProRunId,ProMkt,ProName,ProMinInStock,ProRem,SupName
order by ProMkt

thanks ....

select ProRunId,ProMkt,ProName,ProMinInStock,ProRem,SupName,
 (select Sum (MoveDepQuant) - sum(MoveWithQuant)) as Bal from dbo.Products 
 inner join dbo.Moves on ProRunId=MoveProRunId inner join dbo.Supplier on Products.SupRunId=Supplier.SupRunId
 group by ProRunId,ProMkt,ProName,ProMinInStock,ProRem,SupName
order by ProMkt

Open in new window

Tech_MenAsked:
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you mean something along these lines:

select sum(field) total
  , sum( case when Postive = 1 then field else 0 end ) positive_total
  , sum( case when Postive = 0 then field else 0 end ) negative_total
  ...
0
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
You can try this

select ProRunId,ProMkt,ProName,ProMinInStock,ProRem,SupName,  
 (select Sum (MoveDepQuant) - sum(MoveWithQuant)) as Bal,
Sum(Case When Postive=1 Then MoveDepQuant Else 0 End) as Pos

from dbo.Products  
 inner join dbo.Moves on ProRunId=MoveProRunId inner join dbo.Supplier on Products.SupRunId=Supplier.SupRunId  
 group by ProRunId,ProMkt,ProName,ProMinInStock,ProRem,SupName  
order by ProMkt


Thanks
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
Tech_MenAuthor Commented:
very good
thanks
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 2005

From novice to tech pro — start learning today.