我出来的表格如下,
产品型号 单相 三相 其它 合计
Z401 3 4 1
Z402 7 15 1
通过统计的代码,我想让它成为:
产品型号 单相 三相 其它 合计
Z401 3 4 1 8
Z402 7 15 1 23
合计 10 19 2 31
我的代码:
{$R *.dfm}
{调用过程}
procedure Tchanpinshulianghuizongbiaofm.addItem(xh, gg, sl: string);
var
p: integer; //P的值就是有几种型号
begin
p := idList.IndexOfName(xh); //在列表中查找是否存在这个型号
if p <> -1 then //存在 (如果有这个型号)
begin
p := strtoint(idList.Values[xh]); //把动态表里的有几种型号的值转化成字符赋给P,
if gg = '单相' then
StringGrid1.Cells[1, p] := sl //把凡是单相数量放在第二列 ,至于哪行是由P决定的,
else if gg = '三相' then
StringGrid1.Cells[2, p] := sl
else
StringGrid1.Cells[3, p] :=sl;
end
else
begin
p := StringGrid1.RowCount;
StringGrid1.RowCount := StringGrid1.RowCount + 1; //有一行表头的
StringGrid1.Cells[0, p] := xh;
if gg = '单相' then
StringGrid1.Cells[1, p] := sl
else if gg = '三相' then
StringGrid1.Cells[2, p] := sl
else
StringGrid1.Cells[3, p] := sl;
idList.Add(xh + '=' + inttostr(p)); //把这个型号加到列表中,并且把它在stringgrid中的行号给它
end;
end;procedure Tchanpinshulianghuizongbiaofm.BitBtn1Click(Sender: TObject);
var
intRow: integer; //定义变量行(row)为整型,变量名字为intRow
dxhj,sxhj,qthj,cphj:real; //分别为单相合计 、三相合计、其它合计、产品合计
begin
intRow:=0;
adodataset1.Close;
adodataset1.CommandText :=
'select cpxh_mc,cpgg_mc,sl=count(*) from s_zxx group by cpxh_mc,cpgg_mc ORDER BY count(*)'; //ORDER BY后面表示的字段是表示按这个字段排序
adodataset1.Open;
StringGrid1.RowCount:=1; while not adodataset1.Eof do
begin
addItem(adodataset1.FieldByname('cpxh_mc').AsString,//把上面的过程循环过
adodataset1.FieldByname('cpgg_mc').AsString,
adodataset1.FieldByname('sl').AsString);
adodataset1.Next;
intRow:=intRow+1;
end;
if StringGrid1.RowCount>=1 then
begin
StringGrid1.FixedRows:=1; //如果行树大于等于一行,那么让他显示表头(因为在清除的时候,定义了它只有一行,这样会没有表头掉的),
StringGrid1.RowCount:=StringGrid1.RowCount+1;
StringGrid1.Cells[0,intRow]:='合计' ; //显示合计标签
StringGrid1.Cells[4,intRow]:=FloatToStr(cphj); //显示行合计
end; idList.Clear;
end;
procedure Tchanpinshulianghuizongbiaofm.FormCreate(Sender: TObject);
begin
idList := TstringList.Create;
end;procedure Tchanpinshulianghuizongbiaofm.FormShow(Sender: TObject);
begin
DateTimePicker2.Date:=StrToDate(DateToStr(Now));
StringGrid1.ColCount:=5;
StringGrid1.Cells[0,0]:='产品型号';
StringGrid1.Cells[1,0]:='单相数量';
StringGrid1.Cells[2,0]:='三相数量';
StringGrid1.Cells[3,0]:='其它规格';
StringGrid1.Cells[4,0]:='合计数量';
end;end.
产品型号 单相 三相 其它 合计
Z401 3 4 1
Z402 7 15 1
通过统计的代码,我想让它成为:
产品型号 单相 三相 其它 合计
Z401 3 4 1 8
Z402 7 15 1 23
合计 10 19 2 31
我的代码:
{$R *.dfm}
{调用过程}
procedure Tchanpinshulianghuizongbiaofm.addItem(xh, gg, sl: string);
var
p: integer; //P的值就是有几种型号
begin
p := idList.IndexOfName(xh); //在列表中查找是否存在这个型号
if p <> -1 then //存在 (如果有这个型号)
begin
p := strtoint(idList.Values[xh]); //把动态表里的有几种型号的值转化成字符赋给P,
if gg = '单相' then
StringGrid1.Cells[1, p] := sl //把凡是单相数量放在第二列 ,至于哪行是由P决定的,
else if gg = '三相' then
StringGrid1.Cells[2, p] := sl
else
StringGrid1.Cells[3, p] :=sl;
end
else
begin
p := StringGrid1.RowCount;
StringGrid1.RowCount := StringGrid1.RowCount + 1; //有一行表头的
StringGrid1.Cells[0, p] := xh;
if gg = '单相' then
StringGrid1.Cells[1, p] := sl
else if gg = '三相' then
StringGrid1.Cells[2, p] := sl
else
StringGrid1.Cells[3, p] := sl;
idList.Add(xh + '=' + inttostr(p)); //把这个型号加到列表中,并且把它在stringgrid中的行号给它
end;
end;procedure Tchanpinshulianghuizongbiaofm.BitBtn1Click(Sender: TObject);
var
intRow: integer; //定义变量行(row)为整型,变量名字为intRow
dxhj,sxhj,qthj,cphj:real; //分别为单相合计 、三相合计、其它合计、产品合计
begin
intRow:=0;
adodataset1.Close;
adodataset1.CommandText :=
'select cpxh_mc,cpgg_mc,sl=count(*) from s_zxx group by cpxh_mc,cpgg_mc ORDER BY count(*)'; //ORDER BY后面表示的字段是表示按这个字段排序
adodataset1.Open;
StringGrid1.RowCount:=1; while not adodataset1.Eof do
begin
addItem(adodataset1.FieldByname('cpxh_mc').AsString,//把上面的过程循环过
adodataset1.FieldByname('cpgg_mc').AsString,
adodataset1.FieldByname('sl').AsString);
adodataset1.Next;
intRow:=intRow+1;
end;
if StringGrid1.RowCount>=1 then
begin
StringGrid1.FixedRows:=1; //如果行树大于等于一行,那么让他显示表头(因为在清除的时候,定义了它只有一行,这样会没有表头掉的),
StringGrid1.RowCount:=StringGrid1.RowCount+1;
StringGrid1.Cells[0,intRow]:='合计' ; //显示合计标签
StringGrid1.Cells[4,intRow]:=FloatToStr(cphj); //显示行合计
end; idList.Clear;
end;
procedure Tchanpinshulianghuizongbiaofm.FormCreate(Sender: TObject);
begin
idList := TstringList.Create;
end;procedure Tchanpinshulianghuizongbiaofm.FormShow(Sender: TObject);
begin
DateTimePicker2.Date:=StrToDate(DateToStr(Now));
StringGrid1.ColCount:=5;
StringGrid1.Cells[0,0]:='产品型号';
StringGrid1.Cells[1,0]:='单相数量';
StringGrid1.Cells[2,0]:='三相数量';
StringGrid1.Cells[3,0]:='其它规格';
StringGrid1.Cells[4,0]:='合计数量';
end;end.
upupupupupupupupupuupupupupupupupupupupuup
upupupupupupupupupuupupupupupupupupupupuup
upupupupupupupupupuupupupupupupupupupupuup
upupupupupupupupupuupupupupupupupupupupuup
upupupupupupupupupuupupupupupupupupupupuup
...
循环读入所有要统计的单元格
begin
分析型号,增加相关型号的合计值
分析规格(单相/三相/其它),增加相关规格的合计值
end;
...
何必这么麻烦呢。
FROM test
COMPUTE SUM(a), SUM(b), SUM(c)
其中的a,b,c就相当于你的单相、三相、其它,musum就是合计,
COMPUTE SUM(a), SUM(b), SUM(c)生成最后一行汇总数据,但那个总数(就是右下角的那个)最后需要你用代码生成,这个语句不能产生这个数据。
就一个SQL语句,不是很复杂啊!
SELECT a, b, c, a + b + c AS mysum
FROM test
COMPUTE SUM(a), SUM(b), SUM(c)
只需要把字段名和表名替换为你实际的字段名和表名。