Solved

SQL Sum If

Posted on 2014-04-18
11
610 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

911 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

14 Experts available now in Live!

Get 1:1 Help Now