subtract 2 columns values from two different table

Hi

I am tring to subtract the sum of 2 columns values from two different table but I could not do it

Table A

ID | Cost
101 | $19
103 | $25
102 | $18
106 | $14

Table B

ID | Sell
101 | $24
103 | $28
102 | $21
106 | $16

SELECT (SELECT SUM( “Sell” ) FROM “B”) -
(SELECT SUM( “Cost” ) FROM “A”)
As Binfet

Are the values in the table, $24 or 24? It makes a difference, strings cannot have math applied to them whereas numeric types can.

I would guess they are saved as $24, that would give OP a wrong result

Generally I’d suggest that you leave the currency symbol for the presentation / view. Calculations on numbers and conversions between currencies etc are much easier when you have plain numbers to deal with.

You can of course just skip the first symbol

[code]SELECT (
(SELECT SUM(SUBSTRING(Sell, 2)) FROM B)

(SELECT SUM(SUBSTRING(Cost, 2)) FROM A)
) AS Binfet[/code]

But would I recommend it? Not really, it’s just a hack to make what you have already work.

No, it is INT field
the $ sign I just typed in my post here

In that case are you getting a 0 or an error? Are you looking for net loss/ gain? Because getting the sum of both tables and then subtracting is not the best method of working the difference.

Sponsor our Newsletter | Privacy Policy | Terms of Service