BH    XB   CJ
01    男   4
02    男   3
03    女   5
04    男   5
05    女   4
能否只查一次,就能查出以下结果:
总人数:5
男生数:3
女生数:2
男生总成绩:12 
一次能否查出来,用什么语句?多谢了!!

解决方案 »

  1.   

    SQL.Text := '(select Count(*) from main) union (select Count(*) from main where xb=''男'') union (select Count(*) from main where xb=''女'') union (select Sum(cj) as 男生总成绩 from main where xb=''男'')';下一步分别取出4个值就可以了。
      

  2.   

    最好都写成select Count(*) as ....的形式.
      

  3.   

    select (select count(*)from Table )as 总人数,
    (select count(*)from Table where xb='男')as 男生数,
    (select count(*)from Table where xb='女')as 女生数,
    (select sum(CJ)from Table where xb='男')as 男生总成绩
      

  4.   

    SQL.Text := '(select Count(*) from main) union (select Count(*) from main where xb=''男'') union (select Count(*) from main where xb=''女'') union (select Sum(cj) as 男生总成绩 from main where xb=''男'')';下一步分别取出4个值就可以了。
    请问怎么取这四个值呀
      

  5.   

    select (select count(*)from Table )as 总人数,
    (select count(*)from Table where xb='男')as 男生数,
    (select count(*)from Table where xb='女')as 女生数,
    (select sum(CJ)from Table where xb='男')as 男生总成绩
    是不是这样写呀adoquery1.FieldByName('总人数').AsString;
    adoquery1.FieldByName('男人数').AsString;
    adoquery1.FieldByName('女生数').AsString;
    adoquery1.FieldByName('男人总成绩').AsString;
      

  6.   

    sqlstr='select xb ,cunt=sum(1),sumcj=sum(cj) 
    from tab 
    where 1=1
    group by xb 
    with cube'
      

  7.   

    SQL.Text := '(select Count(*) from main) union (select Count(*) from main where xb=''男'') union (select Count(*) from main where xb=''女'') union (select Sum(cj) as 男生总成绩 from main where xb=''男'')';下一步分别取出4个值就可以了。
    ______________
    查出后,四个值被升序排列了,如
    2
    3
    5
    12
    怎样准确得到呢?
      

  8.   

    SQL.Text := '(select Count(*) from main) union all (select Count(*) from main where xb=''男'') union all (select Count(*) from main where xb=''女'') union all (select Sum(cj) as 男生总成绩 from main where xb=''男'')';改为上面的就不会自动排序了
    下一步知道怎么分别取出值了吧?
      

  9.   

    要分别取出四个值的话可以用Next一条一条的读procedure TForm1.Button1Click(Sender: TObject);
    var
      i: integer;
    begin
      With ADOQuery1 do
      begin
        Close;
        SQL.Text := '(select Count(*) as 总人数 from main) union all (select Count(*) as 男生数 from main where xb=''男'') union all (select Count(*) as 女生数 from main where xb=''女'') union all (select Sum(cj) as 男生总成绩 from main where xb=''男'')';
        Open;
        First;
        while (not eof) do
        begin
          ShowMessage(FieldValues['总人数']);
          Next;
        end;
      end;
    end;
      

  10.   

    感谢cuteant及其它各位朋友,结贴!