请教一个SQL语句 ( 积分: 50 )

  • 主题发起人 主题发起人 liangzhang3942
  • 开始时间 开始时间
L

liangzhang3942

Unregistered / Unconfirmed
GUEST, unregistred user!
我有九个字段,分别为字段1,字段2,字段3,字段4,字段5,字段6,统计1,统计2,统计3
统计1字段统计从字段1到字段6的数据小于11的个数
统计2字段统计从字段1到字段6的数据大于11的个数小于22的个数
统计3字段统计从字段1到字段6的数据大于22的个数小于33的个数
比如说
字段1,字段2,字段3,字段4,字段5,字段6,统计1,统计2,统计3
1 12 15 21 22 29 1 4 1
1 2 11 12 23 25 4 1 2
 
procedure TForm1.Table1CalcFields(DataSet: TDataSet);
var
i:integer;
begin
table1field3.Value:=0;
table1field2.Value:=0;
table1field.Value:=0;
for i:=0 to table1.FieldCount-3 do
begin
if table1.FieldValues[(table1.Fields.FieldName)]<15 then
table1field.Value:=table1field.Value+1;
if (table1.FieldValues[(table1.Fields.FieldName)]>15) and
(table1.FieldValues[(table1.Fields.FieldName)]<30) then
table1field2.Value:=table1field.Value+1;
if (table1.FieldValues[(table1.Fields.FieldName)]>30) and
(table1.FieldValues[(table1.Fields.FieldName)]<50) then
table1field3.Value:=table1field.Value+1;
end;
end;

end.
原数据表字段有六个,用table1连接以后,添加新的字段table1field,table1field2,table1field3.
type 设为float. field typy设为calculated。
 
select FValue00_11=sum(
(case when 字段1<11 then 1 else 0 end)+
(case when 字段2<11 then 1 else 0 end)+
(case when 字段3<11 then 1 else 0 end)+
(case when 字段4<11 then 1 else 0 end)+
(case when 字段5<11 then 1 else 0 end)+
(case when 字段6<11 then 1 else 0 end)
),
FValue11_22=sum(
(case when 字段1>11 and 字段1<22 then 1 else 0 end)+
(case when 字段2>11 and 字段2<22 then 1 else 0 end)+
(case when 字段3>11 and 字段3<22 then 1 else 0 end)+
(case when 字段4>11 and 字段4<22 then 1 else 0 end)+
(case when 字段5>11 and 字段5<22 then 1 else 0 end)+
(case when 字段6>11 and 字段6<22 then 1 else 0 end)
),
FValue22_33=sum(
(case when 字段1>22 and 字段1<33 then 1 else 0 end)+
(case when 字段2>22 and 字段2<33 then 1 else 0 end)+
(case when 字段3>22 and 字段3<33 then 1 else 0 end)+
(case when 字段4>22 and 字段4<33 then 1 else 0 end)+
(case when 字段5>22 and 字段5<33 then 1 else 0 end)+
(case when 字段6>22 and 字段6<33 then 1 else 0 end)
)
from 表1
 
错了,老大,是每行都要统计
 
接受答案了.
 
后退
顶部