开发者

SQL Server: Subtraction between 2 queries

开发者 https://www.devze.com 2023-02-12 07:52 出处:网络
i have 2 queries, where in one table the amount is shown for cars such as Amount_TableCars 800Car A 900Car B

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.

Pl开发者_如何学Goease help!


try something like this:

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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消