关于stringgrid有点问题呢:
比如有这样一个表:name    sex    age    height   study
  a       man    18     20       good
  b       women  20     18       bad
  c       man    18     19       cool
  a       man    21     22       very我这样写一个sql语句:
select name,sex,sum(age) as age,sum(height) as height from message  where name='a'但需要作出这样一个判断,当sum(age) >sum(height) ,将sum(age) -sum(height) ,得到得差额插入到name='a'的那一行,age的那一列,而置height=0。如果sum(age) <sum(height) ,则将差额插入到name='a'的那一行,height的那一列,而置age=0.即是:
——————————————————————————————————————
name      sex        age    height
-------------------------------------------
a         man         0      3
-----------------------------------------------------------------------求求各位了,这个stringgrid对我来说太难了,:)

解决方案 »

  1.   

    是不是要将你要的结果插到stringgrid的最后一行
      

  2.   


    select [name],sex
    ,Case WHEN sum(age)<sum(height) then 0 else (CASE when sum(age)>sum(height) then sum(age) -sum(height) end) end as age
    ,Case when sum(age)>sum(height) then 0 Else (CASE when sum(age)<sum(height) then sum(height)-sum(age) end) end as height 
    from message where name='a' 
    group by [name],sex 
      

  3.   

    谢谢DebugXP,你真厉害,已经解决了。还有就是我查询第二次的时候,怎样把第一次查出的记录清除呢(我第二次查出的结果本来是空集,但是看到的结果还是上次查询留下的)
      

  4.   

    with AdoQuery1 do
      begin
        close;
        sql.Clear;
        sql.Add('上面的SQL');
        open;
      end;
      

  5.   

    不是这样的,我的意思是在第二次查询的时候,我查出的是空值,但stringgrid还保留着原来的数据,我的程序如下:with ADODataSet1 do
     begin
      close;
     CommandText:= ‘上面的sql语句';
      active := True;
      stringgrid1.CleanupInstance;
      First;
      i := 1;    stringgrid1.ColWidths[0] :=100;
        stringgrid1.ColWidths[1] :=100;
        stringgrid1.ColWidths[2] :=100;
        stringgrid1.ColWidths[3] :=100;
      while not Eof do
      begin
        for j :=0 to FieldCount - 2 do
        begin
          // stringgrid1.Cols[i].Add(Fields.Fields[j].AsString);
          stringgrid1.Rows[i].Add(Fields.Fields[j].AsString);
        end;
        i := i + 1;
        Next;
      end;
      begin
        for j :=0 to FieldCount - 2 do
        begin
          // stringgrid1.Cols[i].Add(Fields.Fields[j].AsString);
          stringgrid1.Rows[i].Add(Fields.Fields[j].AsString);
        end;
        i := i + 1;
        Next;
      end;
     //显示标题
      for i := 0 to FieldCount - 1 do
      begin
        stringgrid1.Rows[0].Add(Fields.Fields[i].FieldName);
      end;end;
      

  6.   

    呵呵,贴错了,应该是:
    with ADODataSet1 do
     begin
      close;
     CommandText:= ‘上面的sql语句';
      active := True;
      stringgrid1.CleanupInstance;
      First;
      i := 1;    stringgrid1.ColWidths[0] :=100;
        stringgrid1.ColWidths[1] :=100;
        stringgrid1.ColWidths[2] :=100;
        stringgrid1.ColWidths[3] :=100;
      while not Eof do
      begin
        for j :=0 to FieldCount - 2 do
        begin
          // stringgrid1.Cols[i].Add(Fields.Fields[j].AsString);
          stringgrid1.Rows[i].Add(Fields.Fields[j].AsString);
        end;
        i := i + 1;
        Next;
      end;
       //显示标题
      for i := 0 to FieldCount - 1 do
      begin
        stringgrid1.Rows[0].Add(Fields.Fields[i].FieldName);
      end;end;
      

  7.   

    每次插入数据之前清一下STRINGGRID就可以了啊,你最好对每个单元格赋值为'';
      

  8.   

    stringgrid 是静态的
    你怎么弄都行吧