xorpower
xorpower

Reputation: 18963

SQL Server: Subtraction between 2 queries

i have 2 queries, where in one table the amount is shown for cars such as

Amount_Table        Cars 
800                 Car A
900                 Car B
2100                Car C

Second Table shows discount respectively for Car A, B & C.

Discount_table 
40 
10 
80

I wish to have a final query where in the Amount-Discount values are displayed

The amount table has one query made and discount table has another query. hence i wish to do (amount-query) - (discount query)

I did

Select ( (amount-query) - (discount-query))

but that threw error of
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Please help!

Upvotes: 1

Views: 1004

Answers (3)

BertuPG
BertuPG

Reputation: 653

You cannot "subtract" queries. You have to do a join between tables (or subqueries), and make expressions using columns' names.

Upvotes: 1

Cade Roux
Cade Roux

Reputation: 89661

You need to join:

SELECT *
       ,cars_table.amount - discounts_table.discount
FROM cars_table
INNER JOIN discounts_table
    ON cars.some_key = discounts_table.some_key

Upvotes: 0

Radu Caprescu
Radu Caprescu

Reputation: 993

try something like this:

Select AmountTable.Amount-isnull(DiscountTable.Discount, 0)
from AmountTable left join 
on AmountTable.Car = DiscountTable.Car

Upvotes: 3

Related Questions