好像楼主问了一遍 了吧--测试环境 
create table a1
(
id int,
客户 varchar(10),
购买商品 varchar(10),
价格 int
)create table a2
(
id int,
客户 varchar(10),
付款金额 int
)insert into a1 select 1,    '张三',         '电脑',       100
insert into a1 select 2,    '张三',         '网卡',       200
insert into a1 select 3,    '李四',         '电脑',       300
insert into a1 select 4,    '张三',         '网卡',       200
insert into a1 select 5,    '李四',        '电脑',       300
insert into a1 select 6,    '王五',         '电脑',       300insert into a2 select 1,'张三',     350
insert into a2 select 2,'李四',     320 --付款的select t2.客户,t2.购买商品,t2.价格
from 
(select id,客户,购买商品,价格,hz = (select sum(价格) from a1 where 客户 = t1.客户 and id < = t1.id)
from a1 t1
)t2 , a2
where t2.客户 = a2.客户 and t2.hz <= a2.付款金额--结果
张三电脑100
张三网卡200
李四电脑300--剩余的
select 客户,购买商品,价格
from a1 
where id not in
(select t2.id
from 
(select id,客户,购买商品,价格,hz = (select sum(价格) from a1 where 客户 = t1.客户 and id < = t1.id)
from a1 t1
)t2 , a2
where t2.客户 = a2.客户 and t2.hz <= a2.付款金额
)--结果
张三 网卡 200
李四 电脑 300
王五 电脑 300
--建立视图
create view v_tab 
as
...--把上面的语句写上就可以了

解决方案 »

  1.   

    create table a1(id int,客户 varchar(10),购买商品 varchar(20),价格 int)
    insert a1 select 1,'张三','电脑',100
    union all select 2,'张三','网卡',200
    union all select 3,'李四','电脑',300
    union all select 4,'张三','网卡',200
    union all select 5,'李四','电脑',300
    union all select 5,'王五','电脑',300create table a2(id int,客户 varchar(10),付款金额 int)
    insert a2 select 1,'张三',350
    union all select 2,'李四',320 select x.* from (select *,(select sum(价格) from a1 where 客户=t.客户 and id<=t.id) as 价格1 from a1 t)x left join a2 on x.客户=a2.客户
    where x.价格1>isnull(a2.付款金额,0)
      

  2.   

    select x.* from (select *,(select sum(价格) from a1 where 客户=t.客户 and id<=t.id) as 价格1 from a1 t)x left join a2 on x.客户=a2.客户
    where x.价格1<isnull(a2.付款金额,0)
      

  3.   

    select x.* from (select *,(select sum(价格) from a1 where 客户=t.客户 and id<=t.id) as 价格1 from a1 t)x left join a2 on x.客户=a2.客户
    where x.价格1<=isnull(a2.付款金额,0)sorry?  =