多了一个m 重来四个表 tam tad (主从关系) 由acode关联
ttm ttd (主从关系) 由tcode关联 ttm和tam由acode关联 ttd和tad由tid和did关联借钱主表
tam acode
a01借钱子表 编号为a01的借钱单借了三笔款tad acode did money
a01 0001 300
a01 0002 600
a01 0003 400还钱主表
ttm tcode acode
t01 a01还钱子表 0001和0002对应借钱子表的0001和0002ttd tcode tid tmoney
t01 0001 250
t01 0002 600我的问题是:列出 每一笔借钱的还钱情况 比如这样t01 0001 300 250 50
t01 0002 600 600 0
t01 0003 400 400
最后一列为剩余应该还的钱 怎么办?Waiting Online~~
ttm ttd (主从关系) 由tcode关联 ttm和tam由acode关联 ttd和tad由tid和did关联借钱主表
tam acode
a01借钱子表 编号为a01的借钱单借了三笔款tad acode did money
a01 0001 300
a01 0002 600
a01 0003 400还钱主表
ttm tcode acode
t01 a01还钱子表 0001和0002对应借钱子表的0001和0002ttd tcode tid tmoney
t01 0001 250
t01 0002 600我的问题是:列出 每一笔借钱的还钱情况 比如这样t01 0001 300 250 50
t01 0002 600 600 0
t01 0003 400 400
最后一列为剩余应该还的钱 怎么办?Waiting Online~~
这个对借还只有一条记录的情况,要是有多条,用sum() group by 搞定
借钱主表生成一条记录 子表生成N条记录 为借钱序号did和借钱数量dmoney有人还钱
还钱主表生成一条记录(对应某条借钱主表)
子表生成N条记录 分别对应借钱子表的借钱序号did基本上就是这个意思
insert into @tam select 'a01'declare @tad table(acode varchar(10),did varchar(10),[money] int)
insert into @tad select 'a01','0001',300
insert into @tad select 'a01','0002',600
insert into @tad select 'a01','0003',400declare @ttm table(tcode varchar(10),acode varchar(10))
insert into @ttm select 't01','a01'
declare @ttd table(tcode varchar(10),tid varchar(10),tmoney int)
insert into @ttd select 't01','0001',250
insert into @ttd select 't01','0002',600
select
c.tcode,
b.did,
b.[money],
d.tmoney,
b.[money]-isnull(d.tmoney,0) as 应还
from
@tam a
inner join
@tad b
on
a.acode=b.acode
inner join
@ttm c
on
a.acode=c.acode
left join
@ttd d
on
c.tcode=d.tcode and b.did=d.tid
/*
tcode did money tmoney 应还
---------- ---------- ----------- ----------- -----------
t01 0001 300 250 50
t01 0002 600 600 0
t01 0003 400 NULL 400
*/
t01 0002 600 600 0没有了这条
t01 0003 400 400
declare @tam table(acode varchar(10))
declare @tad table(acode varchar(10),did varchar(10),money int)
declare @ttm table(tcode varchar(10),acode varchar(10))
declare @ttd table(tcode varchar(10),tid varchar(10),tmoney int)insert into @tam select 'a01'insert into @tad select 'a01','0001',300
insert into @tad select 'a01','0002',600
insert into @tad select 'a01','0003',400insert into @ttm select 't01','a01'insert into @ttd select 't01','0001',250
insert into @ttd select 't01','0002',600--执行查询
select
c.tcode,
b.did,
b.money,
d.tmoney,
b.money-isnull(d.tmoney,0) as 应还
from
@tam a
inner join
@tad b
on
a.acode=b.acode
inner join
@ttm c
on
a.acode=c.acode
left join
(select tcode,tid,tmoney=sum(tmoney) from @ttd group by tcode,tid) d
on
c.tcode=d.tcode and b.did=d.tid--输出结果
/*
tcode did money tmoney 应还
---------- ---------- ----------- ----------- -----------
t01 0001 300 250 50
t01 0002 600 600 0
t01 0003 400 NULL 400
*/
ttm ttd (主从关系) 由tcode关联 ttm和tam由acode关联 ttd和tad由tid和did关联借钱主表
tam acode
a01借钱子表 编号为a01的借钱单借了三笔款tad acode did money
a01 0001 300
a01 0002 600
a01 0003 400还钱主表
ttm tcode acode
t01 a01还钱子表 0001和0002对应借钱子表的0001和0002ttd tcode tid tmoney
t01 0001 250
t01 0002 600我的问题是:列出 每一笔借钱的还钱情况 比如这样t01 0001 300 250 50
t01 0002 600 600 0
t01 0003 400 400
最后一列为剩余应该还的钱 怎么办?Waiting Online~~
select tm.tcode,
ad.did, ad.[money],
td.[money],
剩余=isnull(ad.[money],0)-isnull(td.[money],0)
from tam am
inner join tad ad
on am.acode=ad.acode
left join ttm tm
on am.acode=tm.acode
left join ttd td
on tm.tcode=td.tcode
and ad.did=td.tid
ad.did, ad.[money],
td.[money],
剩余=isnull(ad.[money],0)-isnull(td.[money],0)
from tam am
inner join tad ad
on am.acode=ad.acode
left join ttm tm
on am.acode=tm.acode
left join ttd td
on tm.tcode=td.tcode
and ad.did=td.tid
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
谢谢红尘GG~~~~~~~~~~~~~~~~
ttm ttd (主从关系) 由tcode关联 ttm和tam由acode关联 ttd和tad由tid和did关联借钱主表
tam acode
a01借钱子表 编号为a01的借钱单借了三笔款tad acode did money
a01 0001 300
a01 0002 600
a01 0003 400还钱主表 多了一条还钱记录
还可以继续有噢
如果钱没还完的情况下ttm tcode acode
t01 a01
t02 a01 还钱子表 0001和0002对应借钱子表的0001和0002ttd tcode tid tmoney
t01 0001 250
t01 0002 350 t02 0001 10
t02 0002 120 我的问题是:列出 每一笔借钱的还钱情况 比如这样t01 0001 300 260(250+10) 40
t01 0002 600 470(350+120) 130
t01 0003 400 400
最后一列为剩余应该还的钱 怎么办?Waiting Online~~
ad.did, ad.[money],
t.[money],
剩余=isnull(ad.[money],0)-isnull(t.[money],0)
from tam am
inner join tad ad
on am.acode=ad.acode
left join(
select tm.code, td.tid, [money]=sum(td.[money])
from ttm tm
inner join ttd td
on tm.tcode=td.tcode
group by tm.code, td.tid
)t
on am.acode=t.acode
and ad.did=t.tid
------------------------------------------------------------------------------------------------------------------
第二次给你的代码已经考虑了这个情况,所以在对还钱明细表进行了分组统计:(select tcode,tid,tmoney=sum(tmoney) from @ttd group by tcode,tid) d
牛。