比如我的query是这样的:select id,no,value from table1 
where no=1980这样列出所有no=1980的记录,我希望获取value一栏的总和,怎么做?

解决方案 »

  1.   

    select id,no,sum(value) from table1 where no=''1980''
      

  2.   

    select id,no,sum(value) from table1 where no=''1980''
    union 
    select count(id),count(no),sum(value) from table1 where no=''1980''
      

  3.   

    union 
    把两个查询结果全并起来
      

  4.   

    可心用DBGridEh控件呀,它可以实现你想要的结果
      

  5.   

    我是想这样,一个grid显示所有no=1980的记录,下面的一个edit里显示value字段的总和由于做的intraweb开发,没法用eh
      

  6.   

    select id,no,sum(value) from table1 where no=''1980''
    union 
    select count(id),count(no),sum(value) from table1 where no=''1980''
      

  7.   

    谢谢,能在说清楚点吗?求和的edit用那个字段赋值?
    grid里显示哪几个字段?
      

  8.   

    我上面的写的有点错误
    建议字段名别和关键字重名
    tempa//表名
    select id,no1,value1 from tempa where no1=1 
    union 
    select '总和','',sum(value1) from tempa where no1=1
    order by id结果
    -------------------------------------------------------
    id       no1      value1
    1 1 4
    2 1 6
    3 1 9
    总和 19
    -------------------------------------------------------
    procedure TForm1.Button1Click(Sender: TObject);
    begin
        with adoquery1 do
        begin
          last;
          showmessage(inttostr(fieldbyname('value1').AsInteger));///19
        end;
    end;
      

  9.   

    非常感谢qizhanfeng(glacier)的详细解释,我试了下,还有点小问题没解决这样的,我的表是access
     
     table_fin(id,bflag,bno,bvalue,bdate,buser)
       //字段依次为:帐目编号,有效标记,合同编号,金额,日期,经办人 //这个是用来写sql的函数
    procedure SetQFi(str: string);
    begin
        with Datamodule1.Q_Fi do
        begin
            close;
            sql.Clear;
            sql.Add('SELECT * ');
            sql.Add('FROM table_fin ');
            sql.Add('where ');
            sql.Add('bflag=true ');
            sql.Add('and bno=:bno ');
            sql.Add('union');
            sql.Add('select sum(bvalue) as bvalue FROM table_fin');
            sql.Add('where ');
            sql.Add('bflag=true ');
            sql.Add('and bno=:bno ');
            Parameters.ParamValues['bno']:=str;
            open;    end;
    end;    这样出错,说不正常的定义参数对象,提供了不完整或不一致的信息