SQL查询问题,送分啦(200分)

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

lg1

Unregistered / Unconfirmed
GUEST, unregistred user!
SQL查询问题:
表fish有字段:group,vesselname,fdate,fishtype,unitprice,weight,totalprice
每个字段均有重复数据,现要实现报表:
用date1,date2圈定fdate日期范围;edit1.text=group,edit2.text=vesselname;
如何写SQL语句,并用fastreport或reportmachine输出报表?
鱼种规格 平均单价 产量 合计金额
-----------------------
大黄鱼 (重复值取平均) (重复值合计) ...

多谢!!! 200分哪!!
 
你用多条sql语句来实现可以吗?
 
select sum(totalprice)/sum(weight) as AvrPrice,sum(weight) as total weigth
from fish
where fdate between date1,date2 and group=... and vesselname=...
group by 鱼规格字段名
 
同意楼上。
 
select sum(totalprice)/sum(weight) as AvrPrice,sum(weight) as total weigth
from fish
where fdate between date1,date2 and group=... and vesselname=...
group by 鱼规格字段名
注意,用这句当数量为0时将会出现问题 改为以下这句
select case when sum(weight)=0 then 0
else sum(totalprice)/sum(weight) end as AvrPrice ,sum(weight) as total weigth
from fish
where fdate between date1,date2 and group=... and vesselname=...
group by 鱼规格字段名
 
select avg(unitprice) as AvrPrice,sum(weight) as totalweigth,
sum(totalprice) as totalmoney
from fish
where fdate>: date1 and fdate<:date2 and group=... and vesselname=...
group by 鱼规格字段名
 
sum(totalprice)/sum(weight) 是不是值得商议。
如10/5,1/1,单价分别是2,1 平均单价是1.5
如果sum(totalprice)/sum(weight) 呵呵,不是11/6吗?
所以是avg(totalprice/weight)
别的没什么拉!给点分吧!
 
var strdate1,strdate2:string;
ShortDateFormat:='yyyy-mm-dd';
DateSeparator:='-';
strdate1:=formatdatetime('yyyy-mm-dd',date1);
strdate2:=formatdatetime('yyyy-mm-dd',date2);
query.close;
query.sql.clear;
query.sql.add('select fishtype,avg(unitprice) as AvrPrice,sum(weight) as totalweigth,');
query.sql.add('sum(totalprice) as totalmoney');
query.sql.add('from fish');
query.sql.add('where fdate>='''+strdate1+''' and fdate<='''+strdate2+'''');
query.sql.add('and group='''+edit1.text+''' and vesselname='''+edit2.text+'''');
query.sql.add('group by fishtype');
query.prepared:=true;
query.open;
//你不妨試試

 
最好不要用GROUP等名称作为字段名!
 
可是如何反映到fastreoport报表中去呢? 若打印网格,可是怎样将列名称改为中文?如何实现整列的
统计呢?多谢!!
 
date1和date2都由datetimepicker随时产生,怎样设置此日期变量?
 
select case when sum(weight)=0 then 0
else sum(totalprice)/sum(weight) end as AvrPrice ,sum(weight) as total weigth
from fish
where (fdate <:date1)and(fdate>:date2) group=... and vesselname=...
group by 鱼规格字段名
query1.paramsbyname('date1').asdate:=datetimepicker1.date;
query1.paramsbyname('date2').asdate:=datetimepicker2.date;


 
日期问题已解决,可是怎样反映到fastreport 或 machinereport报表?等着拿分数吧!
 
怎么反映,
直接做个打印预览就行了呀!数据源取QUEYR1就行了呀!
 
with TomDModule.Qry_innew do
begin
close;
sql.Clear;
//查询结果错误
//sql.Add('select username as 用户名称, sum(CAST(acctsessiontime as int(8))) as 累计时间单位秒 from tomlists where logtimetotime >= '''+time1+''' and logtimetotime < '''+time2+''' group by username order by username');
//logtiemtotime 为表中的时间型字段
//time1,time2为时间阶段
//试验查询内容 --正确
sql.Add('select username as 用户名称, sum(CAST(acctsessiontime as int(8))) as 累计时间单位秒 from tomlists where logtimetotime between :Date1 and :Date2 group by username order by username');
ParamByName('date1').AsDate:= DateTimePicker1.DateTime; //
ParamByName('date2').AsDate:= DateTimePicker2.DateTime; //date1,date2分别为两个时间段
open;
end;

老兄,借这个问题只是想提醒你,SQL时间查询中存在陷阱,对查询的结果一定要反复验证

如果你能说明错在什么地方,我加分给你了
 
between 的含义是指:(>=小的)or(<=大的)
logtimetotime >= '''+time1+''' and logtimetotime < '''+time2+'''
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~这个是完全的字符串比较!
sql.Add('select username as 用户名称, sum(CAST(acctsessiontime as int(8))) as 累计时间单位秒 from tomlists where logtimetotime between :Date1 and :Date2 group by username order by username');
ParamByName('date1').AsDate:= DateTimePicker1.DateTime; //
ParamByName('date2').AsDate:= DateTimePicker2.DateTime; //date1,date2分别为两个时间段
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这是真正意义上的时间比较!如果你想上面那个也对的话,希望长度两个都相等!大哥,下次注意了!
 
楼上的,你的意思是比较字符串的长度了,如果是这样的话,为什么我的结果有的大多都正确,只有一条
只有一条记录的数值不对,如果时间阶段包含了出错的记录,他的数值也是对的?
我还是不太理解你的说法,
能说的更加清楚一点么,可能还跟deiphl和SQl的时间有关系吧,
 
呵,我再说一下吧!
如果数据库中日期记录为2002-2-1
那么你的比较日期是2001-10-11绝对是2001-10-11>2002-2-1的,即你找不到这条记录!
 
经测试,好象日期字段比较用字符串形式比较准确,而用between..and方式查询结果有偏差!
再请教一个问题:如何用二个按钮使记录在网格中的位置上下移动(调换)?回答着高分奖励!!
 
呵,可能吗?这样就必须改变这两条数据在数据表的排列顺序~
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
700
import
I
后退
顶部