Solved

SQL Sum If

Posted on 2014-04-18
11
604 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now