首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

多表关联解决方法

2012-05-13 
多表关联A 表(销售产品明细表)ID合同号产品编号单价数量111112.050211125.0150311133.0500411212.05051122

多表关联

A 表(销售产品明细表)

ID 合同号 产品编号 单价 数量
1 111 1 2.0 50
2 111 2 5.0 150
3 111 3 3.0 500
4 112 1 2.0 50
5 112 2 3.0 50
6 113 1 2.0 50


B 表(销售回款表)  

ID 合同号 回款金额 回款日期
1 111 1500 2012-2-24
2 112 250 2012-3-4  
3 111 100 2012-4-2


C表(实际发货产品表)

ID 合同号 产品编号 单价 数量
1 111 1 2.0 50
2 111 2 5.0 100
4 112 1 2.0 50
5 112 2 3.0 50


现在要统计出已经发货的(在C表中存在的)但未全部回款的(A表中销售总价不等于B表中回款总金额)合同号

[解决办法]

SQL code
select * from c left join b on b.hetonghao=c.hetonghao inner join a on a.hetonghao=b.hetonghaowhere (a.danjia*b.shuliang) <>b.huikuanjine
[解决办法]
SQL code
select c.hetongNofrom c join (select hetongNo,sum(price*num) amount from a group by hetongNo) b on c.hetongNo = b.hetongNo       join (select hetongNo,sum(reAmount) amount from b group by hetongNo) d on c.hetongNo = d.hetongNowhere b.amount <> c.amountgroup by c.hetongNo
[解决办法]
SQL code
select distinct c.合同号 from c left join a on c.合同号=a.合同号 left join b on c.合同号=b.合同号group by c.合同号having sum(a.单价*数量)<>sum(b.回款金额)
[解决办法]
这个错了,呵呵。
探讨
SQL code


select distinct c.合同号 from c left join a on c.合同号=a.合同号 left join b on c.合同号=b.合同号
group by c.合同号
having sum(a.单价*数量)<>sum(b.回款金额)

[解决办法]
select distinct c.合同号
from a,b,c
where a.合同号=b.合同号
and b.合同号=c. 合同号
and b.回款金额 NOT (a.单价 * a.数量)
[解决办法]
select distinct a.Contractno from A
where a.Contractno not in (
select d.Contractno(
(select b.Contractno,sum(b.price*b.num) as totalb from B b group by b.Contractno) d 
 left join (select c.Contractno,sum(c.backprice) as totalc from C c group by 
c.Contractno) e on e.Contractno = d.Contractno
where d.totalb=e.totalc
)

热点排行
Bad Request.