刚接触delphi,不知如何实现,问题如下:
delphi前台,sql server 后台,
有一个表格,第一列为序号,主键约束或unique约束,总之保证无重复值
            第二列,第三列....为decimal型,小数位3位
            最后还有一个空列,用于存放操作后新生成的结果
表格如下,表名 list,
num    p1     p2     p3      p4      p5     p6
1,   34.760, 9.550, 9.500, 14.250, 24.000 , 0
2,   37.840, 0.780, 8.900, 13.950, 22.000 , 0
3,   36.320, 8.300, 8.900, 14.625, 21.500 , 0
4,   35.360, 9.800, 8.000, 13.725, 22.750 , 0
5,   32.360, 9.350, 8.400, 14.700, 24.500 , 0
...
...
在delphi中,加入了Table,DataSoures,DBGrid以及一个button控件,
程序一运行DBGrid中就显示list表,
button中的代码要实现的功能是 除序号列外,
让每一个值与 本列中 其它值 比大小,值高的在最后一列p6列 值加1,
如果相等两个值的p6列都加1,
举个例子:
p1列的第一个值34.760与本列第二个值37.840比较,则第二行的p6列值加1,
p3列的第二个值8.900与本列第三个值8.900比较,则第二、三行的p6列值都加1,
最后按照p6列的值按降序排列,并保存到一个新表newlist中,newlist表已事先建好
我用c语言通过数组做出来了,但上头说不行,要出报表,
但delphi又刚接触,数据库部分还没接触,照着书上的例子试不出来,
现向各位求救,真的非常紧急,万分感谢
我只能给100分,不足可另帖给分
再三谢过了

解决方案 »

  1.   

    呵呵~~这用SQL就可以解决:(我要满分)select num,p1,p2,p3,p4,p5 case 
    when p1=(select max(p1) from T) then p6+1 
    when p2=(select max(p2) from T) then p6+1
    when p3=(select max(p3) from T) then p6+1
    when p4=(select max(p4) from T) then p6+1
    when p5=(select max(p5) from T) then p6+1
    else 0 end as p6
    from TT: = 你的表。
      

  2.   

    当然,如果p6本身有值的话就这样写了:select num,p1,p2,p3,p4,p5 case 
    when p1=(select max(p1) from T) then p6+1 
    when p2=(select max(p2) from T) then p6+1
    when p3=(select max(p3) from T) then p6+1
    when p4=(select max(p4) from T) then p6+1
    when p5=(select max(p5) from T) then p6+1
    else p6 end as p6
    from T
      

  3.   

    when p1=(select max(p1) from T) then p6+1
    这句sql语句实现的好象是选出p1列的最大值,然后给该纪录的p6列加1
    我要实现的是:
    让每一个值与 本列中 其它值 比大小,值高的(并非最高值)在最后一列p6列 值加1,
    或者说是让所有值与本列其它值两两相比,在每一次比较中,值高的在最后一列p6列值加1,
    如果相等两个值的p6列都加1
    我c语言的算法使用了3层嵌套才实现,只靠sql本身好像是无法实现的吧,初学者,大家多帮助
      

  4.   

    select * from list order by p1;
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      iIndex:integer;
      iMaxNum:integer;
      arrayP1:array[1..5]of Single;
      arrayP2:array[1..5]of Single;
      iP:integer;
      i:integer;
    begin
      with adoquery do
      begin
        Close;
        SQL.Text:='select isnull(min(num),0) as a,isnull(max(num),0) as b from table1';
        Open;
        iIndex:=FieldByName('a').asinteger;
        iMaxnum:=FieldByName('b').asinteger;
        //Close;
        while iIndex<imaxnum do
        begin
          Close;
          SQL.Text:='select top 2 * from table1 where Num>='+inttostr(iIndex);
          Open;
          last;
          iP:=FieldByName('p').asinteger;
          First;
          arrayp1[1]:=FieldByName('p1').value;
          arrayp1[2]:=FieldByName('p2').value;
          arrayp1[3]:=FieldByName('p3').value;
          arrayp1[4]:=FieldByName('p4').value;
          arrayp1[5]:=FieldByName('p5').value;
          Next;
          arrayp2[1]:=FieldByName('p1').value;
          arrayp2[2]:=FieldByName('p2').value;
          arrayp2[3]:=FieldByName('p3').value;
          arrayp2[4]:=FieldByName('p4').value;
          arrayp2[5]:=FieldByName('p5').value;
          for i:=1 to 5 do
          begin
            if arrayp2[i]>arrayp1[i] then
              inc(iP);
          end;
          Close;
          SQl.Text:='update table1 set p='+inttostr(iP)+' where Num='+inttostr(iIndex+1);
          ExecSQL;      inc(iIndex);
        end;  end;
    end;如果NUM不连续,其中再修改一下就可以了,多加几个变量即可
    你看看吧
      

  6.   

    多谢前辈的关心!这两天进展不错,你的思路我初步明白了。
    不过有个问题,我的思路就是取出两个值比大小,但delphi的数值转换很烦人,例如34.76这个值,我用single形变量存,在一个label里显示的是34.759999643.......,不知怎么处理,还望前辈不吝赐教
      

  7.   

    有前途,继续努力啊,呵呵
    格式化输出啊这个函数 FloatToStrF
      

  8.   

    但delphi的数值转换很烦人更正啊,DELPHI的数值转换已经很方便了哦
      

  9.   

    to  xdf_hubei(向太傅):
    也许你已经不再关注这个贴子,但我还是要说一句,前几天瞎忙没时间,今天星期六终于搞出来了,在这里由衷的感谢你对我的帮助!!!!
    我把代码贴在下面了,有很多地方用的都是最笨的方法,不过总算出来了,我的delphi处女作
    从pascal语法开始学到今天完成历时2个星期,觉得自己又笨了许多.....
    procedure TForm1.orderClick(Sender: TObject);
    var
    max : integer;
    num1 : integer;
    num2 : integer;
    count1 : integer;
    count11 : integer;
    count2 : integer;
    count21 : integer;
    arrayP1 : array[1..5] of single;
    arrayP2 : array[1..5] of single;
    begin
    max := query1.RecordCount ;
    for count1 := 1 to (max-1) do
      begin
       query1.First ;
         for count11 := 1 to (count1 - 1) do
             begin
             query1.Next ;
             end;
       num1 := query1.fieldByName('num').Value ;
       arrayP1[1] := query1.FieldByName('p1').value *1000;
       arrayP1[2] := query1.FieldByName('p2').value *1000;
       arrayP1[3] := query1.FieldByName('p3').value *1000;
       arrayP1[4] := query1.FieldByName('p4').value *1000;
       arrayP1[5] := query1.FieldByName('p5').value *1000;   for count2 := 1 to (max - count1) do
         begin
          query1.MoveBy(count2 + count1 - 1);
          num2 := query1.fieldByName('num').Value ;
          arrayP2[1] := query1.fieldbyName('p1').Value *1000;
          arrayP2[2] := query1.fieldbyName('p2').Value *1000;
          arrayP2[3] := query1.fieldbyName('p3').Value *1000;
          arrayP2[4] := query1.fieldbyName('p4').Value *1000;
          arrayP2[5] := query1.fieldbyName('p5').Value *1000;      for count21 := 1 to 5 do
            with query1 do
            begin
             if arrayP1[count21] > arrayP2[count21] then
                begin
                 query1.SQL.Clear ;
                 query1.SQL.Add('update adv');
                 query1.SQL.Add('set p6=p6+1 where num='+inttostr(num1));
                 query1.ExecSQL ;
                 query1.SQL.Clear ;
                 query1.SQL.Add('select * from adv');
                 query1.open;
                end
             else if arrayP1[count21] > arrayP2[count21] then
                     begin
                      query1.SQL.Clear ;
                      query1.SQL.Add('update adv');  //adv --- table name
                      query1.SQL.Add('set p6=p6+1 where num='+inttostr(num2));
                      query1.ExecSQL ;
                      query1.SQL.Clear ;
                      query1.SQL.Add('select * from adv');
                      query1.open;
                     end
                  else
                     begin
                      query1.SQL.Clear ;
                      query1.SQL.Add('update adv');
                      query1.SQL.Add('set p6=p6+1 where num='+inttostr(num2));
                      query1.ExecSQL ;
                      query1.SQL.Clear ;
                      query1.SQL.Add('select * from adv');
                      query1.open;
                     end;
            end;
         end;
      end;
    end;
      

  10.   

    我相信在今天下午7:25分我除掉了这个程序中最后一个bug,当然如果谁发现了新的bug欢迎他(她)指出来,但我所能做的只是在这里表示感谢!
    var
    max : integer;
    num1 : integer;
    num2 : integer;
    count1 : integer;
    count11 : integer;
    count2 : integer;
    count3 : integer;
    arrayP1 : array[1..5] of single;
    arrayP2 : array[1..5] of single;
    begin
    max := query1.RecordCount ;
    for count1 := 1 to (max-1) do
      begin
       query1.First ;
         for count11 := 1 to (count1 - 1) do
             begin
             query1.Next ;
             end;
       num1 := query1.fieldByName('num').Value ;
       arrayP1[1] := query1.FieldByName('p1').value *1000;
       arrayP1[2] := query1.FieldByName('p2').value *1000;
       arrayP1[3] := query1.FieldByName('p3').value *1000;
       arrayP1[4] := query1.FieldByName('p4').value *1000;
       arrayP1[5] := query1.FieldByName('p5').value *1000;   for count2 := 1 to (max - count1) do
         begin
          query1.First ;
          query1.MoveBy(count2 + count1 - 1);
          num2 := query1.fieldByName('num').Value ;
          arrayP2[1] := query1.fieldbyName('p1').Value *1000;
          arrayP2[2] := query1.fieldbyName('p2').Value *1000;
          arrayP2[3] := query1.fieldbyName('p3').Value *1000;
          arrayP2[4] := query1.fieldbyName('p4').Value *1000;
          arrayP2[5] := query1.fieldbyName('p5').Value *1000;      for count3 := 1 to 5 do
            with query1 do
            begin
             if arrayP1[count3] > arrayP2[count3] then
                begin
                 query1.SQL.Clear ;
                 query1.SQL.Add('update adv');
                 query1.SQL.Add('set p6=p6+1 where num='+inttostr(num1));
                 query1.ExecSQL ;
                 query1.SQL.Clear ;
                 query1.SQL.Add('select * from adv');
                 query1.open;
                end
             else if arrayP1[count3] < arrayP2[count3] then
                     begin
                      query1.SQL.Clear ;
                      query1.SQL.Add('update adv');
                      query1.SQL.Add('set p6=p6+1 where num='+inttostr(num2));
                      query1.ExecSQL ;
                      query1.SQL.Clear ;
                      query1.SQL.Add('select * from adv');
                      query1.open;
                     end
                  else
                     begin
                      query1.SQL.Clear ;
                      query1.SQL.Add('update adv');
                      query1.SQL.Add('set p6=p6+1 where num='+inttostr(num1)+' or num='+inttostr(num2));
                      query1.ExecSQL ;
                      query1.SQL.Clear ;
                      query1.SQL.Add('select * from adv');
                      query1.open;
                     end;
            end;
         end;
      end;
    end;