Solved

SQL Sum If

Posted on 2014-04-18
11
627 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 65

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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 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 65

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 65

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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.

749 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