SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vack
Constraint Violating Yak Guru

USA
497 Posts

Posted - 10/16/2012 :  11:02:56  Show Profile  Reply with Quote
I have a table iminvloc that will have multiple records for one item_no.


item_no  loc   QtySold  ReOrder
ABC       1     100       40
ABC       2     200       55
ABC       3     400       2

What I want to do is sum the qtysold for all locations but only pull the ReOrder from location 1. 

select item_no, sum(qtySold) as Total sold, (Reorder from location 1??
from iminvloc_sql

I would want my results to be:

ABC    700     40



jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 10/16/2012 :  11:19:33  Show Profile  Reply with Quote
SELECT item_no, sum(QTY_sold) as QtySold,sum(case when loc = 1 then reorder else 0 end) as reOrder
FROM iminvloc
GROUP BY item_no

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Vack
Constraint Violating Yak Guru

USA
497 Posts

Posted - 10/16/2012 :  11:43:03  Show Profile  Reply with Quote
What if I had a fourth location but I only wanted the Location 1, 2 and 3?
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 10/16/2012 :  12:11:33  Show Profile  Reply with Quote
Then your case statement wuold be something like
SUM(CASE WHEN loc in(1,2,3) THEN ReOrder ELSE 0 END) or
SUM(CASE WHEN loc <> 4 THEN ReOrder ELSE 0 END)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000