日期:2014-05-18 浏览次数:20615 次
create table 发货表(发货日 datetime,客户 varchar(10),日发货金额 numeric(12,4)) go insert 发货表 select '2007-07-01','何健铭', 77745.00 insert 发货表 select '2007-08-05','金和兴', 74120.00 insert 发货表 select '2007-08-06','李克秋', 33834.00 insert 发货表 select '2007-09-01','伦祖坤', 24055.00 go create table 收款表(收款日 datetime,客户 varchar(10),日收款金额 numeric(12,4)) go insert 收款表 select '2007-07-01','何健铭',77745.00 insert 收款表 select '2007-08-15','金和兴',70000.00 insert 收款表 select '2007-08-26','李克秋',33834.00 insert 收款表 select '2007-11-05','伦祖坤',20000.00 ---1 select 客户,发货总额,收款总额,应收欠款, 利息=(case when datediff(d,发货日,收款日) between 3 and 30 then 应收欠款*0.0085 when datediff(d,发货日,收款日) BETWEEN 30 and 44 then 应收欠款*0.001 when datediff(d,发货日,收款日) BETWEEN 45 and 60 then 应收欠款*0.00125 when datediff(d,发货日,收款日) >61 then 应收欠款*0.0015 else 0 end) from ( select a.客户,max(a.发货日) as 发货日,max(b.收款日) as 收款日,发货总额 = sum(日发货金额), 收款总额 = sum(日收款金额), sum(日发货金额 - 日收款金额) as 应收欠款 from 发货表 a left join 收款表 b on a.客户 = b.客户 group by a.客户 ) a /* 客户 发货总额 收款总额 应收欠款 利息 ---------- ---------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- 何健铭 77745.0000 77745.0000 .0000 .000000 金和兴 74120.0000 70000.0000 4120.0000 35.020000 李克秋 33834.0000 33834.0000 .0000 .000000 伦祖坤 24055.0000 20000.0000 4055.0000 6.082500 (所影响的行数为 4 行) */ ----2 select a.客户,发货总额 = sum(日发货金额), 收款总额 = sum(日收款金额), sum(日发货金额 - 日收款金额) as 应收欠款, 利息=sum((case when datediff(d,发货日,收款日) between 3 and 30 then 日发货金额*0.0085 when datediff(d,发货日,收款日) BETWEEN 30 and 44 then 日发货金额*0.001 when datediff(d,发货日,收款日) BETWEEN 45 and 60 then 日发货金额*0.00125 when datediff(d,发货日,收款日) >61 then 日发货金额*0.0015 else 0 end)) from 发货表 a left join 收款表 b on a.客户 = b.客户 group by a.客户 /*客户 发货总额 收款总额 应收欠款 利息 ---------- ---------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- 何健铭 77745.0000 77745.0000 .0000 .000000000 金和兴 74120.0000 70000.0000 4120.0000 630.020000000 李克秋 33834.0000 33834.0000 .0000 287.589000000 伦祖坤 24055.0000 20000.0000 4055.0000 36.082500000 (所影响的行数为 4 行) */ drop table 发货表, 收款表
------解决方案--------------------
也不知道结果对不对,你试了再说吧.今天加班,随便写了一下.晚上再看.
/*借老龟的数据*/ create