问一个简单的Local sql问题?(50分)

  • 主题发起人 主题发起人 eastweast
  • 开始时间 开始时间
E

eastweast

Unregistered / Unconfirmed
GUEST, unregistred user!
我想统计出每天总的数量,程序如下:
但是运行时报告,不正确的列名:dayseq,有没有其他比较简单的方法?
之前我用大量的循环实现,结果导致死机。

with Query1 do begin
close;
sql.clear;
sql.add('SELECT EXTRACT(DAY FROM INDATE) AS dayseq,SUM(MOUNT) FROM "DIARY.DB"');
sql.add('WHERE (INDATE>=:Fromdate) and (INDATE<=:Todate)');
sql.add('GROUP BY dayseq ORDER BY dayseq');
parambyname('Fromdate').asdate:=datetimepicker1.Date;
parambyname('Todate').asdate:=datetimepicker2.Date;
prepare;
open;
end;
其中INDATE是一个日期型字段,MOUNT是一个整形字段。
 
const
QryStr='select Extract(Day From InDate) as DaySeq,Sum(Mount) from "Diary.db" '+
'Where (InDate Between "%s" and "%s") Group by InDate order by InDate'
begin
with Query1 do begin
SQL.Text:=format(QryStr,
[FormatDateTime('mm''/''dd''/''yyyy', DateTimePicker1.Date),
FormatDateTime('mm''/''dd''/''yyyy', DateTimePicker2.Date)]);
Prepare;
Open;
end
end

其中FormatDateTime('mm''/''dd''/''yyyy',...
主要用于写Paradox表,如果数据库是SQL Server 7,
那么换成 FormatDateTime('yyyy-mm-dd',...

 
我试过了,不行!
提示如下:"Field in order by must in resule set!"
我用下面方法解决:
sql.add('SELECT INDATE,MAX(EXTRACT(DAY FROM INDATE)) AS dayseq,SUM(MOUNT) FROM "DIARY.DB"');
sql.add('WHERE (INDATE>=:Fromdate) and (INDATE<=:Todate)');
sql.add('GROUP BY INDATE ORDER BY INDATE');
parambyname('Fromdate').asdate:=datetimepicker1.Date;
parambyname('Todate').asdate:=datetimepicker2.Date;
虽然解决了,还是有点不爽,有没有其他更好的办法?
 
group by 的列必须存在于 select 语句
 
select Day(indate) as dayseg,SUM(MOUNT) as vy from Diary.db
...
Group by Day(indate);
 
with Query1 do begin
close;
sql.clear;
sql.add('SELECT EXTRACT(DAY FROM INDATE) AS dayseq,SUM(MOUNT) FROM "DIARY.DB"');
sql.add('WHERE (INDATE>=:Fromdate) and (INDATE<=:Todate)');
sql.add('GROUP BY EXTRACT(DAY FROM INDATE) ORDER BY EXTRACT(DAY FROM INDATE)');
parambyname('Fromdate').asdate:=datetimepicker1.Date;
parambyname('Todate').asdate:=datetimepicker2.Date;
prepare;
open;
end;
 
多人接受答案了。
 
后退
顶部