Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculation in Query

Posted on 2009-04-08
9
Medium Priority
?
355 Views
Last Modified: 2013-12-12
I am trying to run a query against a MYSQL DB that is giving me the wrong calculation.
4 fields.. A is a total of B, C and D so that leaves me with the total and 3 of the 4 other values but i also need to calculate for the 4th value by subtracting values B,C, and D from the A..after that i need to multiply the values against a number and add it all together? Here is the formula that im using in case that makes no sense which i doubt it will...

(A-B-C-D) + (B*2) + (C*3) + (D*4) = Answer
Remember A is a total of B, C and D

not as complicated as my question i hope...any ideas the syntax to use?

here is the Query im trying which is giving the wrong result


SELECT ((A-B-C-D)+(B*2)+(C*3)+(D*4)) AS total FROM DB

0
Comment
Question by:akalbfell
[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
  • 5
  • 3
9 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 600 total points
ID: 24101384
>Remember A is a total of B, C and D
does that not mean that:
A-B-C-D

should return 0 ?

please clarify with data samples what you get, and what you should get?
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1400 total points
ID: 24101393
Please give sample of data and expected results as your formula looks like it should do what you want assuming you want 2 x B, 3 x C and 4 x D to be the result.

Since A = B + C + D, then A - B - C - D = 0 has to be true.

Simply your formula is:
SELECT (B*2) + (C*3) + (D*4) AS total FROM DB;
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24101402
:) LOL -- Guess I agree with Angle Eyes.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 8

Author Comment

by:akalbfell
ID: 24101456
Here is an example of a row of data...

A= 180
B= 39
C= 4
D= 20

So the value i dont have which im calculating at the start is 180-39-4-20 which is 117
then i need to multiply B * 2(39*2), C * 3,(4*3) and D * 4(20*4)....Then add those 3 numbers to 117 which should give me my answer, 287,  but instead my query produces 207
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24101494
Ah, ok.  This works fine for me.
SELECT (A - B - C - D) + (B * 2) + (C * 3) + (D * 4) AS Total
FROM (
	SELECT 180 AS A
	, 39 AS B
	, 4 AS C
	, 20 AS D
) t

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1400 total points
ID: 24101507
Try this and post a snippet of the results:
SELECT A, B, C, D, ((A-B-C-D)+(B*2)+(C*3)+(D*4)) AS Total
FROM DB

Open in new window

0
 
LVL 8

Author Comment

by:akalbfell
ID: 24101549
Still produces 207 not 287
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24101603
But post the line as it comes out from the query I posted.  It is hard to help when can't reproduce the problem and can't see what you are getting.

This simplier formula even worked for me as just reduce the number of B's, C's, and D's adding back by 1 and not subtract anything from A.

A, B, C, D, Total
180, 39, 4, 20, 287

Above is the result I get from my test query.
SELECT A, B, C, D, (A+B+(C*2)+(D*3)) AS Total
FROM DB

Open in new window

0
 
LVL 8

Author Closing Comment

by:akalbfell
ID: 31568234
ran it again just for sanity measures and it worked...nothing looks different...very strange...anyhow thanks guys, appreciate the quick responses
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

721 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