我有一个KC_KC2库存:里面的记录为:
prod_no(商品编码),whouse_no(库房编号),amt(库存数量),whouse(库房名)
10001 05 1000 库房1
10002 01 200 库房2
10001 10 500 库房3一个gg_spbm商品编码表:
prod_no(商品编码),old_type(原型号),new_type(新型号)
10001 100 6000
10002 121 6210
在查询时我想实现:prod_no 库房1 库房2 库房3 总库存
10001 1000 0 500 1500
10002 0 200 0 200
prod_no(商品编码),whouse_no(库房编号),amt(库存数量),whouse(库房名)
10001 05 1000 库房1
10002 01 200 库房2
10001 10 500 库房3一个gg_spbm商品编码表:
prod_no(商品编码),old_type(原型号),new_type(新型号)
10001 100 6000
10002 121 6210
在查询时我想实现:prod_no 库房1 库房2 库房3 总库存
10001 1000 0 500 1500
10002 0 200 0 200
解决方案 »
- 中英文和字符混合的情况下,如何定长截断字符串,比如:定长截断60位?
- 关于数据库安装出错的问题??????在线等急.......
- 在线求助?如何用SQL语句实现表中的某个字段按记录号递增?
- 在一个空间上鼠标的形状变成手型在哪里设啊
- 谁有《Delphi 6电子商务应用开发》的电子版?
- ADOQuery1.Delete该怎样用,我为什么删除不了数据,反而出现了错误!
- 关于dbgrid的两个问题,请大家帮忙!!
- 用dbedit控件连接一个字段,该字段的类型为浮点数,为什么dbedit控件显示的不是该浮点数,而是一堆整数呢?例字段值为0.2,显示的是2980232;可当字段值为0.5时,显示的就是0.5
- 动态创建菜单的问题!
- 郁闷哪,好多接口不知道怎么用
- 关于读文件的问题!!
- delphi的报表问题 (急急急,线上等待)
var
sSQL: String; i:integer;
begin
ssql:='';
with dm.kc_Query2 do
begin
close;
SQL.Clear;
SQL.Add('Select whouse from kc_kc2 group by whouse order by whouse');
open;
for i:=1 to dm.kc_query2.recordcount do
begin
if i<> dm.kc_query2.recordcount then
sSQL:= trim('(select amt from kc_kc2 where prod_no=g.prod_no and whouse='''+FieldByName('whouse').asstring+''') as '+FieldByName('whouse').asstring+',')
else
sSQL:= trim('(select amt from kc_kc2 where prod_no=g.prod_no and whouse='''+FieldByName('whouse').asstring+''') as '+FieldByName('whouse').asstring);
next;
with dm.kc_query1 do
begin
close;
sql.clear;
if edit1.Text='' then
begin
sql.add('select DISTINCT (k.prod_no) as 商品编号,old_type as 原型号,new_type as 新型号,avg_price as 价格,prod_area as 产地,(SELECT SUM(k.amt) FROM kc_kc2 k WHERE k.prod_no = g.prod_no) AS 总库存,'+ssql+' FROM kc_kc2 k,gg_spbm g where k.prod_no=g.prod_no ORDER BY k.prod_no');
dm.kc_query1.Open;
end end;
end;
end;
用SQL Server做后台一点儿问题都没有,Access却不行,请各位高手指点!
(
select Prod_No,Sum(amt) as t1 ,0 as t2 ,0 as t3,0 as t4 from kc_kc2 where whouse = '库房1' group by prod_no
union all
select Prod_No,0 as t1 ,Sum(amt) as t2 ,0 as t3,0 as t4 from kc_kc2 where whouse = '库房2' group by prod_no
union all
select Prod_No,0 as t1 ,0 as t2 ,Sum(amt) as t3,0 as t4 from kc_kc2 where whouse = '库房3' group by prod_no
union all
select Prod_No,0 as t1 ,0 as t2 ,0 as t3,Sum(amt) as t4 from kc_kc2 group by prod_no) t
group by Prod_No
好像不行,我的whouse(库房名)字段不是固定的,它是动态增加的。还有,上面的SQL语句出现别名't1'循环引用的错误我该怎么办呀?
set @sql = 'select Prod_No,'
select @sql = @sql + 'sum(case whouse when '''+whouse+'''
then amt else 0 end) as '''+whouse+''','
from (select distinct whouse from kc_kc2) as a
set @Sql = @Sql + 'Sum(amt) as Total'
select @sql = @Sql + ' from kc_kc2 group by Prod_No'
exec(@sql)
一定能将它在 Access 中解决 !
IN做子查询...不要用EXIST这个MSSQL关键字..
to hansion3406(阿木) :谢谢,左联或右联在ACCESS中好像不好用呢,请请教!
v : Integer ;procedure Updates(var s : String ; i,j : Integer) ;
begin
Delete(s,(i - 6) ,8) ;
Insert('Sum(amt) as t' + IntToStr(j) + ', ',s,(i - 6)) ;
if j = v then
Delete(s,Pos(IntToStr(j),s) + 1,1) ;
end ;procedure TForm1.Button2Click(Sender: TObject);
var
s1,s2,STemp,SZero : String ;
i,j,w : Integer ;
begin
j := 0 ;
s1 := 'select Prod_No, ' ;
SZero := 'select Prod_No , ' ;
with ADOQuery1 do
begin
Close ;
Sql.Text := 'select distinct whouse from Kc_Kc2 ' ;
Open ;
First ;
v := RecordCount + 1 ;
for i := 1 to v do
begin
s1 := s1 + 'Sum(t' + IntToStr(i) + ') as t' + IntToStr(i) + ',' ;
SZero := SZero + '0 as t' + IntToStr(i) + ',' ;
end ;
s1 := Copy(s1,1,Length(s1) - 1) ;
SZero := Copy(SZero,1,Length(SZero) - 1) ;
s1 := s1 + ' from ' ;
SZero := SZero + ' from ' ;
s2 := SZero ;
STemp := s1 + #13#10 + '( ' + #13#10 ;
while Not Eof do
begin
Inc(j) ;
w := Pos(IntToStr(j),s2) ;
if w > 0 then
begin
Updates(s2,w,j) ;
s2 := s2 + ' kc_kc2 where whouse = ''' + FieldByName('whouse').AsString + ''' group by prod_no '
+ #13#10 + 'Union all ' + #13#10 ;
STemp := STemp + s2 ;
end ;
s2 := SZero ;
Next ;
end ;
end ;
Inc(j) ;
w := Pos(IntToStr(j),s2) ;
if w > 0 then
begin
Updates(s2,w,j) ;
s2 := s2 + 'kc_kc2 group by prod_no' + #13#10 + ') t' + #13#10 + 'group by Prod_No' ;
STemp := STemp + s2 ;
ShowMessage(STemp) ;
Memo1.Text := STemp ;
end ;
with ADOQuery1 do
begin
Close ;
Sql.Text := STemp ;
Open ;
First ;
end ;
end;
此刻的我都不知该说些什么了,真的实现了,你真聪明!以后你就作我的军师吧,如何?明天再结贴吧。
从以上内容我看不出什么太多情况,简单分析一下,就是你的数据库在导入ACCSEE后,数据的完整性是否重新检验,或者主键之类是否重新检查一下。布置你为什么非要改用ACCSEE,SQL SERVER不是很好吗?
在同一台机器上也可以运行,如果是SQL的版权问题,你使用MSDE不就OK了?性能也比ACCSEE强的多,如果你的数据量比较大,那用ACCSEE,将来更是个麻烦。
// Sql.Text := STemp ; 前 Arrange(STemp) ;procedure TForm1.Arrange(var s : String) ;
var
Ts : TStrings ;
i,j : Integer ;
s1 : String ;
begin
i := 0 ;
Ts := TStringList.Create ;
Ts.Text := s ;
s1 := Ts.Strings[0] ;
with ADOQuery1 do
begin
Close ;
Sql.Text := 'select distinct whouse from Kc_Kc2 ' ;
Open ;
First ;
while Not Eof do
begin
Inc(i) ;
j := Pos('as t' + IntToStr(i) , s1) ;
if j > 0 then
begin
System.Delete(s1, j,5) ;
System.Insert('as ' + ADOQuery1.FieldByName('whouse').asString,s1,j) ;
end ;
Next ;
end ;
end ;
Inc(i) ;
j := Pos('as t' + IntToStr(i) , s1) ;
if j > 0 then
begin
System.Delete(s1, j,5) ;
System.Insert('as ºÏ¼Æ',s1,j) ;
end ;
Ts.Strings[0] := s1 ;
s := Ts.Text ;
Ts.Free ;
end ;