与结算确切匹配待处理金额

时间:2015-07-11 13:46:58

标签: sql-server-2008

我有两个表@pending表是等待结算的待处理基金表,@ set表是为了解决这个@pending但是需要匹配每个记录的确切全部金额(值)。但我无法弄清楚如何执行此任务。任何人都有想法。

declare @pending as table (id int, value decimal(18,4))
declare @settle as table (id int, value decimal(18,4))

insert into @pending (id, value) values (1, 500)
insert into @pending (id, value) values (2, 250)
insert into @pending (id, value) values (3, 100)
insert into @pending (id, value) values (4, 250)

insert into @settle (id, value) values (1,500)
insert into @settle (id, value) values (2,300)
insert into @settle (id, value) values (3,200)

期望结果: 以下记录不符合

insert into @pending (id, value) values (3, 100)

以下记录必须匹配

insert into @pending (id, value) values (1, 500)
insert into @pending (id, value) values (2, 250)
insert into @pending (id, value) values (4, 250)

insert into @settle (id, value) values (1,500)
insert into @settle (id, value) values (2,300)
insert into @settle (id, value) values (3,200)

   row | pending_id | pending_value | pending_bal | match_amount | settle_id 
    ------------------------------------------------------------------
    1       1              500           0             500           1           
    2       2              250           0             250           2          
    3       4              250           200            50           2
    4       4              250           0             200           3

1 个答案:

答案 0 :(得分:0)

您可以加入值列:

SELECT p.id, p.value, s.id, s.value
FROM   @pending p
JOIN   @settle s ON p.value = s.value