大家好,我想用DELPHI查询SQL中多个表的汇总,请问相关语句该如何写啊?
我的目的是这样的:
按日期段查询:表1(20080101mx)..表N(2008010Nmx)中相同记录的数据的汇总并生成两张新表(一张明细汇总,一张种类汇总).
例:表1:
名称 数量 单价 金额 种类
青菜 10   0.5  5  蔬菜
白菜 20   0.3  6  蔬菜
鸡肉 20   3.0  60 肉类
鸭肉 10   4.0  40 肉类
草鱼 15   3.0  45 水产类
...... 
表N:
名称 数量 单价 金额 种类
青菜 15   0.6  9  蔬菜
白菜 25   0.2  5  蔬菜
鸡肉 25   3.0  75 肉类
苹果 10   2.0  20 水果类
金桔 15   1.0  15 水果类目的一生成明细汇总表:(这里单价为平均单价)如果在表1到表N中的有不同记录的数据也加入汇总名称 数量 单价 金额 种类
青菜 20   0.55 10  蔬菜
白菜 45   0.25 11  蔬菜
鸡肉 45   3.0  135 肉类
鸭肉 10   4.0  40  肉类
草鱼 15   3.0  45  水产类 
苹果 10   2.0  20  水果类
金桔 15   1.0  15  水果类目的二生成种类汇总表:
种类   数量   金额
蔬菜   65    21
肉类   55    175
水产类 15    45
水果类 25    35  谢谢大家,我是新手,请好心人帮帮忙吧.

解决方案 »

  1.   


    --目的一生成明细汇总表:(这里单价为平均单价)如果在表1到表N中的有不同记录的数据也加入汇总 select 名称 ,  数量 ,  avg(单价)as '单价',   金额 ,  种类 
    from
        (
         select * from t1
           union all
         select * from t2
           union all
         select * from t3
           ...
           union all
         select * from tn
        )a
    where 条件
    group by
       名称, 数量 , 金额 , 种类 
    --目的二生成种类汇总表: select  种类 , sum(数量)as '数量' ,sum(金额)as '金额'  
    from
        (
         select * from t1
           union all
         select * from t2
           union all
         select * from t3
           ...
           union all
         select * from tn
        )a
    group by
        种类 
      

  2.   

    您好,我想实现的是按日期段查询,也就是动态选择表1到表N,也就是说语句中的select * from t1 union all select * from tN,tN是不固定的由选择的日期段确定,该如何写?是不是要设一个循环语句啊?
      

  3.   

    1.用动态SQL按相应的日期拼成相应的表名
    2.用楼上的SQL来处理
    注:在拼SQL完后要用EXEC (SQL) 或 Exec executesql来处理动态字符串
      

  4.   

    简单写了一个你参照一下
    首先创建一个临时表 tmptable
    将要查询日期内的数据存入这个临时表中
      EndDate := DateTimePicker1.Date;
      while EndDate<=DateTimePicker2.Date do
      begin
        //把数据存在临时表中
        with AdoQuery1 do
        begin
          close;
          sql.text := ' insert into '+tmptable+ ' select * from '+formatdatetime('yyyymmdd',now())+'mx';
          Execsql;
        end;
        EndDate := DateTimePicker1.Date+1;
      end;
    然后根据临时表,查询你要出的两个报表
    select 名称 ,  数量 ,  avg(单价)as '单价',   金额 ,  种类 
    from tmptable
    where 条件
    group by 名称, 数量 , 金额 , 种类 目的二生成种类汇总表: 
    select  种类 , sum(数量)as '数量' ,sum(金额)as '金额'  
    from tmptable
    group by
        种类
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      StrList:TStringList;
      I:Integer;
    begin
      StrList:=TStringList.Create;
      I:=1;
      With ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select name from sysobjects where xtype=''U'' and SubString(name,1,7)='''+日期过滤+''' ' );//SubString(name,1,7)是截取出来的日期.我不知道你表名具体是怎样的所以写了是1 TO 7个字符.
        Open;
      end;
      ADOQuery1.First;
      with ADOQuery1 do
      begin
        While not Eof do
        begin
          StrList.Add(' Select 名称   数量   单价   金额   种类 from '+FieldByName('Name').AsString+' ');
          if I<>ADOQuery1.RecordCount then
          StrList.Add(' Union all ');
          Inc(I);
          Next;
        end;
      end;
      ShowMessage(StrList.Text);
    end;
    剩下的自己在解决