rishabhpatel
asked on
MS Access Query: Multiply two fields
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!
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!
ASKER
That will get too long for a large query... I want to use a solution with an if statement
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is it ok if I write a small prodedure?
Amulya.
Amulya.
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?
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
First and second both soulusion are correct, I would prefer IIF() one.
Best of luck
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.
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.
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'