麻烦大家帮我看看我的多表联查代码那里有问题

taxueliuhong 2011-09-05 10:28:33

SQLStr := 'select Qc.CoutName,Qc.QcYingSK,Qc.QcYiSK,Bq.BqYingSK,Bq.BqYiSK,'+
'Qc.QcYingSK+Bq.BqYingSK-Qc.QcYiSK-Bq.BqYiSK as QmYingSK from '+

' (select c.CoutName,sum(a.TNumber*a.Price) as QcYingSK,'+
' sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as QcYiSK '+
' 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 < :StartDate ';

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 c.CoutName) Qc';

SQLStr := SQLStr +' full outer join on Qc.CoutName=Bq.CoutName '+

' (select c.CoutName,sum(a.TNumber*a.Price) as BqYingSK,'+
' sum(iif(isnull(b.BcMoney),0,b.BcMoney)) as BqYiSK '+
' 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 c.CoutName) Bq ';

with qrySkYsZkHzCout do
begin
Close;
SQL.Clear;
SQL.Add(SQLStr);
Parameters.ParamByName('StartDate').Value:=edtBeginCout.Value;
Parameters.ParamByName('EndDate').Value:=edtEndCout.Value;
Open;
end;


这些代码需要在Access上查询,查询出来就效果就是:
客户|期初应收款|期初已收款|本期应收款|本期已收款|期末未收款
里面三个表的关系,请查看http://topic.csdn.net/u/20110904/13/7427b16f-0e7d-4690-8060-86038f4f83af.html?599454753
...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
晓蛋 2011-09-06
  • 打赏
  • 举报
回复
为啥咱这个板块人气这么旺呢?
taxueliuhong 2011-09-05
  • 打赏
  • 举报
回复
其实Access与MSSQL基本上差不多的,哪位大哥能在sql上跑出结果,大概也是很有参考价值的。方便的话,那怕帮帮忙写个sql的查询也可以,谢谢大家了。我到access那里去,很没有人气,帖子已经发了,可以还没有回复的。
--小F-- 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]
你应该去ACCESS版问.
[/Quote]
.
chuanzhang5687 2011-09-05
  • 打赏
  • 举报
回复
+1[Quote=引用 1 楼 dawugui 的回复:]

你应该去ACCESS版问.
[/Quote]
-晴天 2011-09-05
  • 打赏
  • 举报
回复
没有测试环境,最好你自己在机器上在ACCESS的查询界面上测试语句,这样比较容易了解是否正确.
dawugui 2011-09-05
  • 打赏
  • 举报
回复
你应该去ACCESS版问.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧