Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Sum If

Posted on 2014-04-18
11
Medium Priority
?
667 Views
Last Modified: 2014-04-18
Can someone convert this Access query to sql please.

SELECT Material.Material, Sum(Material_Location.On_Hand_Qty) AS OH, Sum(IIf([job].[status]=’active’,[order_quantity],0)) AS WIP
FROM (Material LEFT JOIN Material_Location ON Material.Material = Material_Location.Material) LEFT JOIN Job ON Material.Material = Job.Part_Number
WHERE (((Material.Class) Like ‘fghwkb’))
GROUP BY Material.Material
HAVING (((Sum(Material_Location.On_Hand_Qty))>0));

THANKS.
0
Comment
Question by:PreciseITDept
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40009331
Give this a whirl..
SELECT m.Material, Sum(ml.On_Hand_Qty) AS OH, Sum(CASE WHEN status='active', THEN order_quantity ELSE 0 END) AS WIP
FROM Material m
   LEFT JOIN Material_Location ml ON m.Material = ml.Material
   LEFT JOIN Job j ON m.Material = j.Part_Number
WHERE m.Class Like 'fghwkb'
GROUP BY m.Material
HAVING Sum(ml.On_Hand_Qty) >0

Open in new window

btw I have an article out there on Migrating your Access Queries to SQL Server Transact-SQL which is a grab-bag of Access SQL to T-SQL.

Hope this helps.
Jim
0
 

Author Comment

by:PreciseITDept
ID: 40009386
I'll give it a go.  Thanks!
0
 

Author Comment

by:PreciseITDept
ID: 40009391
Jim, here's what I got:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40009404
typo, remove the comma after status='active'
SELECT m.Material, Sum(ml.On_Hand_Qty) AS OH, Sum(CASE WHEN status='active' THEN order_quantity ELSE 0 END) AS WIP
FROM Material m
   LEFT JOIN Material_Location ml ON m.Material = ml.Material
   LEFT JOIN Job j ON m.Material = j.Part_Number
WHERE m.Class Like 'fghwkb'
GROUP BY m.Material
HAVING Sum(ml.On_Hand_Qty) >0

Open in new window

0
 

Author Comment

by:PreciseITDept
ID: 40009420
Now this error:
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'status'.

So I made it job.status and then got this:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "job.status" could not be bound.
0
 

Author Comment

by:PreciseITDept
ID: 40009424
So I made it j.status and voila!!
THANKS BIG TIME!  I'll check out your migrating site.  Only do these now and then.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40009428
>Ambiguous column name 'status'.
In the case block, you'll have to spell out which table's status column you are testing, as the error message means that there are multiple tables in your FROM block that have a column named status.

>So I made it job.status and then got this:
Since I'm using the alias j, try j.status
0
 

Author Comment

by:PreciseITDept
ID: 40009511
Alas, the results were not accurate.  Too many loops thru the Job table I believe.  Here are three Access queries which actually bring the correct results.  (first two are called in the third)  If you can convert them to one SQL it would be very appreciated.  I'll award another 500 points, or more, if I can.  Or should I make it a new question?   Thanks.

SELECT Material.Material, Sum(Material_Location.On_Hand_Qty) AS OH
FROM Material LEFT JOIN Material_Location ON Material.Material = Material_Location.Material
WHERE (((Material.Class) Like "fghwkb"))
GROUP BY Material.Material
HAVING (((Sum(Material_Location.On_Hand_Qty))>0))
ORDER BY Material.Material;

SELECT Material.Material, Material.Class, Sum(Job.Order_Quantity) AS SumOfOrder_Quantity
FROM Material LEFT JOIN Job ON Material.Material = Job.Part_Number
WHERE (((Job.Status)="active"))
GROUP BY Material.Material, Material.Class
HAVING (((Material.Class)="fghwkb"));

SELECT Material.Material, Material.Class, [Honeywell on hand].OH, [Honeywell current WIP].SumOfOrder_Quantity
FROM (Material LEFT JOIN [Honeywell current WIP] ON Material.Material = [Honeywell current WIP].Material) LEFT JOIN [Honeywell on hand] ON Material.Material = [Honeywell on hand].Material
WHERE (((Material.Class)="fghwkb"));
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40009603
New question please.  I'm out for the next couple of hours.
0
 

Author Comment

by:PreciseITDept
ID: 40009618
Jim, If you want to hold off a bit I'm streamlining this whole thing.  S/ make it much easier to understand.  Thanks for the help thus far.
0
 

Author Comment

by:PreciseITDept
ID: 40009619
OK.  Will make it a new question.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question