【发布时间】:2011-09-27 17:28:20
【问题描述】:
我使用的是 MS SQL Server,我有 2 个表。
供应清单
sl_id(pk) supply_id(fk)* transaction_id Qty
1 14 872670099 3
2 15 872670100 5
3 16 872670101 1
4 16 872670105 4 <
- supply_id 是 amenity 表中 supply_id 的外键
供应
supply_id(pk) no_of_units
----------------------------
13 2
14 3
15 6
16 10
输出应该是supply_id,然后是no。可用单位数等于 No_of_units 减去数量。
输出
id units available
-------------------------
13 2
14 0 --> [1]
15 1
16 5 --> [2]
[1] 由于基于 supply_list 表 supply_id 14 的数量为 3
[2] 有两条记录包含 supply_id 16,所以我们必须将它们的数量相加,它们是 4 和 1,所以我们有 5。从 supply_id 16 的 no_of_units 中减去 5,我们将得到 5 的单位可用。
【问题讨论】:
-
@OMG Ponies——感谢编辑:D
标签: sql sql-server stored-procedures aggregate-functions