我有一个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

解决方案 »

  1.   

    代码如下:
    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却不行,请各位高手指点!
      

  2.   

    不同的数据库支持不同的SQL,大概是ACCESS不支持你的SQL语句。
      

  3.   

    是的,access的sql语句和mssql是不同的,你的报错是什么啊?怎么你的字段还有用中文的啊改一下把
      

  4.   

    to  WWWWA(aaaa) :谢谢你!可我们一定要用它来实现这个功能。to  myfine(myfine) :谢谢你!提示的错误是数据提供程序或其他服务返回E_FAIL状态,与用中文的字段没关系,在SQL Server中是一点儿问题也没有的呀!
      

  5.   

    access对子查询的支持好象有问题
      

  6.   

    如果你用的是标准的Sql语句而没有依靠Sql Server扩展的Sql语句以及函数应该没问题.请查看你的Sql语句 。
      

  7.   

    JJ :   用 Union All 来连接稍等
      

  8.   

    // Access Case 语句也不支持我只知道用 Union 了select Prod_No,Sum(t1) as t1,Sum(t2) as t2,Sum(t3) as t3, Sum(t4) as t4 from
    (
    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
      

  9.   

    DD:
    好像不行,我的whouse(库房名)字段不是固定的,它是动态增加的。还有,上面的SQL语句出现别名't1'循环引用的错误我该怎么办呀?
      

  10.   

    如果库房动态在 Access 中也许 ... , Study ---------------------------------------------------Sql Server 2000 中的实现(库房动态) :declare @sql varchar(500)
    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)
      

  11.   

    别急我再找别的方法:) 
    一定能将它在 Access 中解决 !
      

  12.   

    JOIN...左联或右联..
    IN做子查询...不要用EXIST这个MSSQL关键字..
      

  13.   

    to  wjlsmail(计算机质子) :实在是感动...临帖涕泠...
     to  hansion3406(阿木) :谢谢,左联或右联在ACCESS中好像不好用呢,请请教!
      

  14.   

    你试一试把你的第二个sql语句精简一下~舍弃一些东西,看看哪儿出错,那后在加回来~不要那么多显示内容,在逐步分析一下哪儿错了,我感觉没有用多少关键字啊~
      

  15.   

    // JJ : 代码没有整理,简单测试了一下,觉得可以作为动态交叉表的一种做法var
      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;
      

  16.   

    My DD:
       此刻的我都不知该说些什么了,真的实现了,你真聪明!以后你就作我的军师吧,如何?明天再结贴吧。
      

  17.   

    请楼主介绍一下具体症状,以便大家分析。
    从以上内容我看不出什么太多情况,简单分析一下,就是你的数据库在导入ACCSEE后,数据的完整性是否重新检验,或者主键之类是否重新检查一下。布置你为什么非要改用ACCSEE,SQL SERVER不是很好吗?
    在同一台机器上也可以运行,如果是SQL的版权问题,你使用MSDE不就OK了?性能也比ACCSEE强的多,如果你的数据量比较大,那用ACCSEE,将来更是个麻烦。
      

  18.   

    to  jpyc(智若愚) :谢谢你!其实你的提议很好,可是我们的领导非得要求我们这么做要给单机板的小型用户来使用,数据库的完整性没什么问题的,还有在制作SQL安装时用到MSDE,我不知道该怎么来用希望赐教!再次感谢!
      

  19.   

    // 整理字段名 
    //  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 &ordm;&Iuml;&frac14;&AElig;',s1,j) ;
      end ;
      Ts.Strings[0] := s1 ;
      s := Ts.Text ;
      Ts.Free ;
    end ;