Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

How to correctly compute a value based on two fields

I have a table with items for sale, along with the quantity for each item.  I am trying to correctly come up with accounting statistics, but so far I am unable to take into consideration the quantity when computing total value.

This is what I have right now, which is clearly not correct:
 
selectStatement = "select sum(Cost) from tBooks where Stat = 'For Sale' ";

Open in new window


Is there a way I can have Firebird do the computation for Cost, taking into consideration the Quantity also (multiply Cost by Quantity)?
ASKER CERTIFIED SOLUTION
Avatar of Nick Upson
Nick Upson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

Perfect...thanks Nick
Avatar of rmmarsh

ASKER

Oops... doesn't compute on all of the qualified rows...
what exactly do you mean by "doesn't compute"
Avatar of rmmarsh

ASKER

Turns out I was using the wrong field for Quantity... this is what it's supposed to be, however I originally defined NbrOfCopies as varchar(3) and now it's causing me problems...

Here is what I have:

 
select sum(Price * NbrOfCopies) as totalValue from tBooks where Stat = 'For Sale'

Open in new window

Avatar of rmmarsh

ASKER

I tried doing a "cast(NbrOfCopies as integer)", but it didn't like that either...
Avatar of rmmarsh

ASKER

Oh, one more question:  

Can I do something like this without screwing up the existing data?

 
ALTER TABLE tBooks ALTER NbrOfCopies TYPE integer

Open in new window

Avatar of rmmarsh

ASKER

Well, I found out that I can't do ALTER TABLE as above... is there some way I can define a new column of type integer and move the data from NbrOfCopies (which is varchar (3) to the new column?
Avatar of rmmarsh

ASKER

Please ignore the above questions... opening a new question...