现在我有三个表,一个是主表,另外两个是从表。主表的结构由三个字段“BillCode(单号)/Billdate(单据日期)/Depot(仓库)”组成;第一个从表由三个字段”BillCode(单号)/Number(数量)/Price(单价)“组成;第二个从表由三个字段”BillCode(单号)/ObjBillCode(业务单号)/TotalMoney(总金额,由第一个从表的明细项目的SUM(Number*Price)获得)/BcMoney(本次收款金额)“组成。第一,第二个主从表组成送货单;第一,第三个主从表组成收款单。注意:送货单的单号跟收款单的单号是相互独立的,三个表的关联关系是"表1.BillCode=表2.BillCode=表3.ObjBillCode”。问题是:如何在不使用临时表的情况下,使用一条查询语句,查出结构如“BillCode/TotalMoney/SUM(BcMoney)”的结果?实际上就是根据送货单,查询出送货单的回款情况。
select ObjbillCode,TotalMoney,sum(BcMoney) from 第二个从表 group by ObjbillCode,TotalMoney
from 第一个从表 a left join 第二个从表 b on a.BillCode=b.ObjBillCode
group by a.billCode
您这个代码也很有启发性,如果只统计有回款的项目,这个是很简洁的代码的。只是现在的情况比较麻烦,有些送货单从来没有汇过款,也需要列举出来。
这个能解决,我需要过滤主表中,depot=“第一成品仓”,或者制单日期为“2011-09-01”到“2011-09-30”吗?我上面的提问是为了简洁,实质上,主表需要提供条件过滤的。
from 第一个从表 a left join 第二个从表 b on a.BillCode=b.ObjBillCode
where exists(select 1 from 主表 where billCode=a.billCode and depot='第一成品仓' and 制单日期 between '2011-08-01' and '2011-09-30')
group by a.billCode
{ SQLStr := 'select *,TInNumber-TOutNumber as THjNumber,TInMoney-TOutMoney as THjMoney from('+
'select a.CoutName,'+
'sum(iif( a.Identifier=''采购进货'',b.TNumber,0)) as TInNumber,'+
'sum(iif( a.Identifier=''采购进货'',b.Price*b.TNumber,0)) as TInMoney,'+
'sum(iif( a.Identifier=''采购退货'',b.TNumber,0)) as TOutNumber,'+
'sum(iif( a.Identifier=''采购退货'',b.Price*b.TNumber,0)) as TOutMoney '+
'from T_Bill a inner join T_BillDetail b on a.BillCode=b.BillCode '+
'where addDate between :StartDate and :EndDate '; } SQLStr := 'select a.billCode,sum(a.Number*a.Price) as TotalMoney,sum(isnull(b.BcMoney,0)) as FinishMoney '+
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill where billCode=a.billCode '+
' and addDate between :StartDate and :EndDate '; if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and a.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and a.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and a.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +')group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;
提示用“于函数参数的个数不对 在查询表达式sum(isnull(b.BcMoney,0))中 ”
FinishMoney:已经完成的金额
T_Bill:送货退货单表头,兼任收款付款单表头
T_BillDetail:送货退货单表体
T_SFBillDetail:收款付款单表体
这个不会错的.
{ SQLStr := 'select *,TInNumber-TOutNumber as THjNumber,TInMoney-TOutMoney as THjMoney from('+
'select a.CoutName,'+
'sum(iif( a.Identifier=''采购进货'',b.TNumber,0)) as TInNumber,'+
'sum(iif( a.Identifier=''采购进货'',b.Price*b.TNumber,0)) as TInMoney,'+
'sum(iif( a.Identifier=''采购退货'',b.TNumber,0)) as TOutNumber,'+
'sum(iif( a.Identifier=''采购退货'',b.Price*b.TNumber,0)) as TOutMoney '+
'from T_Bill a inner join T_BillDetail b on a.BillCode=b.BillCode '+
'where addDate between :StartDate and :EndDate '; } SQLStr := 'select a.billCode,sum(a.Number*a.Price) as TotalMoney '+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate '; if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +')group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;这样改,那么就提示“至少一个参数没有被指定值”,我是Access数据库,有关系吗?
SQLStr := 'select a.billCode,sum(a.TNumber*a.Price) as TotalMoney '+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate '; if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +')group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;这个写法终于可以查询出结果了,目前剩下两个问题,一个是结果中没有主表的日期参数,也没有已经收款的金额。。
直接改下面的,不能查到发货单的日期的: SQLStr := 'select c.,addDate,a.billCode,sum(a.TNumber*a.Price) as TotalMoney '+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate '; if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +')group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;
sum(NZ(b.BcMoney,0))
我使用下面的语句可以查询,但是查询出来的结果不对: SQLStr := 'select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,'+
' sum(iif(isnull(b.BcMoney),b.BcMoney,0)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(iif(isnull(b.BcMoney),b.BcMoney,0)) as NotFinishMoney'+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate ';上面的结构,FinishMoney应该是有数据的,但是结果是零,而那个减法,不计算结果。
我QQ是36519719,能加我吗?
把下面这句拷到ACCESS的查询SQL界面去运行一下试试:
select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,
sum(nz(b.BcMoney,0)) as FinishMoney,
sum(a.TNumber*a.Price) - sum(nz(b.BcMoney,0)) as NotFinishMoney,
sum(nz(b.BcMoney,0)) as FinishMoney
from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode
where exists(select 1 from T_Bill c where c.billCode=a.billCode
and c.addDate between "2011-09-01" and "2011-09-30"
select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,
sum(nz(b.BcMoney,0)) as FinishMoney,
sum(a.TNumber*a.Price) - sum(nz(b.BcMoney,0)) as NotFinishMoney,
sum(nz(b.BcMoney,0)) as FinishMoney
from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode
where exists(select 1 from T_Bill c where c.billCode=a.billCode
and c.addDate between "2011-09-01" and "2011-09-30")
' sum(nz(b.BcMoney,0)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(nz(b.BcMoney,0)) as NotFinishMoney '+
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode ' +
' where exists(select 1 from T_Bill c where c.billCode=a.billCode ' +
' and c.addDate between "2011-09-01" and "2011-09-30" '; if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +') group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
//Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
//Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;仍然提示没有定义的函数NZ
SQLStr := 'select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,'+
' sum(iif(isnull(b.BcMoney),b.BcMoney,0)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(iif(isnull(b.BcMoney),b.BcMoney,0)) as NotFinishMoney'+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate ';这组代码能查到结果,就是结果不对。FinishMoney有值得是0,null是null
SELECT a.BillCode, SUM(a.TNumber * a.Price) AS TotalMoney, SUM(ISNULL(b.BcMoney, 0))
AS FinishMoney, SUM(a.TNumber * a.Price) - SUM(ISNULL(b.BcMoney, 0))
AS NotFinishMoney
FROM T_BillDetail a LEFT OUTER JOIN
T_SFBillDetail b ON a.BillCode = b.ObjBillCode
WHERE EXISTS
(SELECT 1
FROM T_Bill c
WHERE c.billCode = a.billCode AND c.addDate BETWEEN '2011 - 08 - 01' AND
'2011 - 09 - 30')
GROUP BY a.BillCode我刚才特意到MSSQL里测试,确实是可以用的代码!就差没有addDatel了!!!
可以在这里看你的SQL语句是否正确,是否能查出正确的结果.
是我搞错了,ACCESS支持exists.
正确的语句是 SQLStr := 'select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,'+
' sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as NotFinishMoney'+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate ';
if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +') group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;sum(iif(isnull(b.BcMoney),0,b.BcMoney)) 刚才的写法,把为空值的情况写反了!!!!
现在就差让结果显示C.AddDate,老大再帮帮忙噻!
再连接一个主表就行了.SELECT a.BillCode,c.addDate, SUM(a.TNumber * a.Price ...
from ...
left join 主表 on a.billCode=c.billCode
...
group by a.billcode,c.adddate
到底是如何结合下面原有的代码的?麻烦您最说明白一点,我先谢谢您了! SQLStr := 'select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,'+
' sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as NotFinishMoney'+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate ';
if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.CoutName like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +') group by a.billCode'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;
SQLStr := 'select a.billCode,sum(a.TNumber*a.Price) as TotalMoney,'+
' sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as NotFinishMoney'+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode '+
' where exists(select 1 from T_Bill c where c.billCode=a.billCode '+
' and c.addDate between :StartDate and :EndDate '; } SQLStr := 'select a.billCode,c.addDate,sum(a.TNumber*a.Price) as TotalMoney,'+
' sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as FinishMoney,'+
' sum(a.TNumber*a.Price) - sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as NotFinishMoney'+ //,sum(isnull(b.BcMoney,0)) as FinishMoney
' from (T_BillDetail a left join T_SFBillDetail b on a.BillCode=b.ObjBillCode) '+
' left join T_Bill c on a.BillCode=c.BillCode '+
' where c.addDate between :StartDate and :EndDate '; if cbbBillType.Text ='销售发货' then SQLStr := SQLStr + ' and c.Identifier=''销售发货'' ';
if cbbBillType.Text ='销售退货' then SQLStr := SQLStr + ' and c.Identifier=''销售退货'' ';
if cbbBillType.Text ='发货退货' then SQLStr := SQLStr + ' and (c.Identifier=''销售发货'' or c.Identifier=''销售退货'') ' ; if cbbDepot.Text <>'' then SQLStr := SQLStr +
' and c.Depot like '+ Quotedstr('%'+cbbDepot.Text+'%'); if cbbSalesPurchase.Text <>'' then SQLStr := SQLStr +
' and c.SalesPurchase like '+ Quotedstr('%'+cbbSalesPurchase.Text+'%'); if cbbBillUser.Text <>'' then SQLStr := SQLStr +
' and c.BillUser like '+ Quotedstr('%'+cbbBillUser.Text+'%'); SQLStr := SQLStr +' group by a.billCode,c.addDate'; with qrySkYsZkHzBill do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginBill.Value;
Parameters.ParamByName('EndDate').Value:=edtEndBill.Value;
Open;
end;