复杂的sql语句问题
------------------------------------------------------------------------------------
if object_id('tempdb..#temp3') is not null drop table #temp3
select T_Sell.CustomerNo, t_contract.contractno,T_Customer.CustName,SellName,sum(Suttle) as Suttle,deptName ,count(t_contract.contractno) as cnum
into #temp3
from T_Sell,T_SellKind,T_Customer,t_contract
where T_Sell.SellId=T_SellKind.SellId and T_Sell.CustomerNo=T_Customer.CustomerNo
and SellMode='合同'
group by T_Sell.CustomerNo,CustName,SellName,deptName,t_contract.contractno
select * from #temp3
=====================================================================
我建一张临时表,(#temp3) T_Sell表(销售表)(里面有字段(CustomerNo[客户号],ContractNo[合同号],Suttle[净重]) SellMode[销售方式],deptName[部门名称])
T_Customer(客户)表(里面有字段(CustomerNo[客户号],ContractNo[合同号],CustName[客户名称])
T_SellKind表(品种表)(里面有字段(SellId [品种编号],SellName[品种名] )
这里的cnum是统计合同份数
t_contract表(合同表 )(里面的字段有(CustomerNo[客户号],ContractNo[合同号])
--------------------------------------------------------------------------------------
执行上面的sql结果如下:
----------------------------------------------------------------------------
(客户号) (合同号) (客户名称) (品种) 净重 部门
customerno contractno custname SellName Suttle (deptName)
KH00000002 HT00000003 北京xxx贸有限公司 衣服 68698.30 销售
KH00000004 HT00000143 西安xxx有限公司 水果 300077.15 销售
cnum
(合同份数)
13
12
-------------------------------------------------------------------------
我想在合同表里面匹配到~的是每个客户签了多少分合同?但是我发现cnum的数据不太,不知道我错到那里了?
请问大家这个sql语句应该怎么写?我把分用完了,只有10分了:-)