?
Solved

SQL Sum If

Posted on 2014-04-18
11
Medium Priority
?
653 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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