统计怎么写(25分)

  • 主题发起人 主题发起人 zikao419
  • 开始时间 开始时间
Z

zikao419

Unregistered / Unconfirmed
GUEST, unregistred user!
是这样的:在一个表中有时间字段,和编号字段,我查询在一定
时间段内相同编号的记录数,两个时间是由两个datatimepick控件
分别附近程序里的,注意:没有起始时间
 
select count(*) from table
where time between time1 and time2
group by id
可以知道这个时间端不通的编号各有多少!
 
select id,count(*) from tablename
where time between time1 and time2
group by id
//
 
select ID, count(ID) from table
where time between time1 and time2
group by id
 
select distinct id,count(id) from tablename
where time between time1 and time2
group by id
 
大家都没有明白我的意思,我的意思是这样的
表:
流水账号 编号 姓名 日期
0000001 0001 a 2000-9-8
0000002 0002 b 2000-6-7
0000003 0001 a 2000-7-13
我现在通过两个datatimepick控件输入两个时间,比如
2000-7-8和2000-10-3这个时间段察看每一个编号的纪录数
并且要显示记录数大于3条以上的纪录(通过dbgrid)
 
察看每一个编号的纪录数
Select 编号 As ID,count(编号) As Cnt From tablename
Where time Between time1 And time2
Group By id

显示记录数大于3条以上的纪录编号
Select ID
From
(
Select 编号 As ID,count(编号) As Cnt From tablename
Where time Between time1 And time2
Group By id
) As t1
Where Cnt>3

显示记录数大于3条以上的纪录
Select *
From YourTableName As t2,
(
Select ID
From
(
Select 编号 As ID,count(编号) As Cnt From tablename
Where time Between time1 And time2
Group By id
) As t1
Where Cnt>3
) As t3
Where (t2.编号=t3.ID) And (time Between time1 And time2)

建议聚合结果表t1保存临时表。
 
to yuleijun

我不明白“建议聚合结果表t1保存临时表。”是什么意思?
 
没什么其他意思。
如果需要在这个时间段多次查询,建议保存结果,因为聚合运算比较慢.如果可能,尽量利用以前的成果。
当然如果这个时间段的数据会更新,那还是必须临时查询。
 
你的要求这样就可以了,先去试
SELECT *
FROM table
WHERE (time Between time1 And time2)
AND 编号 IN
(SELECT 编号
FROM table
WHERE time Between time1 And time2
GROUP BY 编号
HAVING COUNT(编号) >3)
 
真是不好意思,可能是我没有表达清楚我的意思吧

表:
流水账号 编号 姓名 日期
0000001 0001 a 2000-9-8
0000002 0002 b 2000-6-7
0000003 0001 a 2000-7-13
0000004 0003 c 2000-7-25
0000005 0001 a 2000-3-16

我们对这个表要实现的是常客的统计
比如:用户是定在一个月内光顾本店两次视为
常客(由用户自定义)然后查询出结果。我的
思路是这样的:首先查询编号为0001的纪录数,
然后在检查向另两条纪录的时间差是不是一个月
若是,则符合条件显示纪录,但是这样有一个问题
若编号有几千条呢,这就需要频繁的打开关闭adoquery
是需要很长时间的,谁能帮我写一个比较好的sql语句呢?
 
SELECT ID, COUNT(ID)
FROM Table
WHERE time > time1 AND time < time2
HAVING COUNT(ID)>2
 
你的“常客”标准是过去曾经有一次一个月内光顾本店两次;还是最近光顾本店两次在一个月内?
如果是前者,先做好统计放到另一个表里,不要临时查。
 
用Filter方法打开表
然后统计
//完毕后取消Filter打开表
 
后退
顶部