Solved

MS Access Query: Multiply two fields

Posted on 2004-08-12
7
2,189 Views
Last Modified: 2008-02-01
I have a DB  in MS Access:

Fields:
Batch - Text
Amount - Currency
Percent - Number
Sold - Yes/No


Batch        Amount         Percent       Global
---------------------------------------------------
A5774B    4556.25          90              Yes
C55647    4435.85          85              No
A5774B    5456.55          100            Yes

-------------------------------------------------------------------------------------

I want to make a query that will output the (Amount * Percent/100)
If it is global it will be in the global fiel else in the sub field

viz.

Batch        Global             Sub
---------------------------------------
A5774B    4100.63        
C55647                          3785.77
A5774B    5456.55

solution could can be in sql as well!
0
Comment
Question by:rishabhpatel
7 Comments
 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
Try this :

SELECT Batch, Amount*Percent/100 as Global, NULL as Sub
FROM MyTable
WHERE Global = 'Yes'
UNION
SELECT Batch, NULL as Global, Amount*Percent/100 as Sub
FROM MyTable
WHERE Global = 'No'
0
 

Author Comment

by:rishabhpatel
Comment Utility
That will get too long for a large query... I want to use a solution with an if statement
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 250 total points
Comment Utility
SELECT Batch, iif(Global = 'Yes',Amount*Percent/100,NULL) as Global2, iif(Global = 'No',Amount*Percent/100,NULL) as Sub2
FROM MyTable
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:amulya_333
Comment Utility
Is it ok if I write a small prodedure?

Amulya.
0
 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
I'm not sure what a procedure would benefit - it would basically contain the same SQL. If you split the query, you are going to re-create either the union approach, or the iif approach, which will do the same thing, but be less efficient?
0
 
LVL 1

Expert Comment

by:anupbhunia
Comment Utility
I don't think it would enhance the performance if you create a procedure and compile it, as the query is without greate complexity.

First and second both soulusion are correct, I would prefer IIF() one.

Best of luck
0
 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
I suspect the union method will be quicker particularly if there is an index on the 'Global' field, as the IIF has to evaluate 2 function calls per row, as opposed to dividing the table in 2 via an index search.
As for performance of procedure - the time here will be in the actual calc, not the compilation etc so there is no benefit in that regard.
If performance becomes an issue it is indexing that will solve it.

Can you explain the term
"That will get too long for a large query... "

long queries are only a problem for programmers, and if it is A union B you just copy/paste the two halves. The database won't mind a long text string.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 SQL - Query Missing Value 10 49
vcenter 6 u2 install question 1 85
MS Access question 11 48
oracle query help 18 75
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now