+ -
当前位置:首页 → 问答吧 → 一个挺简单的问题,如何进行统计

一个挺简单的问题,如何进行统计

时间:2011-12-14

来源:互联网

有一个 table

有 id, recvdate , value 3个字段


有 1000 多个 不同的id
每个相同 id 有 200-300 条记录

现在 需要 统计

id max(recvdate) , value ( max(recvdate) 时候的 ),max(recvdate) , value(max(recvdate) 时候的), min(value), recvdate( min(value) 时候的recvdate) , max(value) , recvdate( max(value) 时候的recvdate))

看起来简单,但是 做起来比较麻烦。 有办法吗?

作者: im2web   发布时间: 2011-12-14

作者: fredrickhu   发布时间: 2011-12-14

你自己写的输出列都重复了

作者: guguda2008   发布时间: 2011-12-14

楼主啥意思呀

作者: lzd_83   发布时间: 2011-12-14

SQL code
select a.*,b.* from(
select id,value1 as maxvalue1,value2 as maxvalue2,recvdate as maxrecvdate from tb a where not exists(select 1 from tb where id=a.id and recvdate>a.recvdate)
)a inner join (
select id,value1 as minvalue1,value2 as minvalue2,recvdate as minrecvdate from tb a where not exists(select 1 from tb where id=a.id and recvdate<a.recvdate)
)b on a.id=b.id

作者: qianjin036a   发布时间: 2011-12-14

用存储器分步求解:
你的这个问题,只要先获得id+最大recvdate+最小recvdate,然后再查主表,就比较简单。

以前有个相似的问题,就是统计每个月的里程表。大致与你类似。

作者: chengg0769   发布时间: 2011-12-14

啥意思

作者: ssp2009   发布时间: 2011-12-14

描述得不够清楚吗?

需要取得
select id , recvdate , value where id = xx order by recvdate desc limit 1;
select id , recvdate , value where id = xx order by recvdate asc limit 1;
select id , recvdate , value where id = xx order by value desc limit 1;
select id , recvdate , value where id = xx order by value desc limit 1;

这样 4 个结果
合成一个结果行。

然后 每个id 都需要这么取


其实就是取出 历史记录中 最大值 最小值 对应的历史时间 以及 最早 和最晚 历史时间 的值。

4个结果的组合

作者: im2web   发布时间: 2011-12-14

SQL code
/*MYSQL的?*/
SELECT *
FROM (
select id , recvdate , value where id = xx order by recvdate desc limit 1
) T1
INNER JOIN  (
select id , recvdate , value where id = xx order by recvdate asc limit 1
) T2 ON T1.ID=T2.ID
INNER JOIN  (
select id , recvdate , value where id = xx order by value desc limit 1
) T3 ON T1.ID=T3.ID
INNER JOIN  (
select id , recvdate , value where id = xx order by value desc limit 1
) T4 ON T1.ID=T4.ID

作者: guguda2008   发布时间: 2011-12-14

SQL code

select
  *
from
(select id , recvdate , value where id = xx order by recvdate desc limit 1)a
inner join
(select id , recvdate , value where id = xx order by recvdate asc limit 1)b
on
 a.id=b.id
inner join
(
select id , recvdate , value where id = xx order by value desc limit 1
)c
on
  b.id=c.id
inner join
(
select id , recvdate , value where id = xx order by value desc limit 1
)d
on
  c.id=d.id

作者: fredrickhu   发布时间: 2011-12-14

大侠们 多谢 但是 我要的是每个id 的统计

id = xx 不是单个的么?

单个id 可以手动run 4个sql 然后把结果合并起来。

我要的是group by id 的做法

作者: im2web   发布时间: 2011-12-14

给出表结构和数据吧,猜很累的~~

作者: gogodiy   发布时间: 2011-12-14

CREATE TABLE `abcd` (
`id` INT(10) NULL DEFAULT NULL,
`date` INT(10) NULL DEFAULT NULL,
`value` INT(10) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT


INSERT INTO `abcd` (`id`, `date`, `value`) VALUES
(1, 1, 10),
(1, 2, 4),
(1, 1, 9),
(1, 2, 1),
(1, -1, 1),
(1, 4, 1),
(1, 10, 4);


差不多就是这个样子

作者: im2web   发布时间: 2011-12-14

给出测试数据和期望的结果吧。猜很累的~

作者: stublue   发布时间: 2011-12-14

简单的和股票对比就还很清楚了

id 是股票的时间  
date 是某个时间点
value 是某个时间点的股价


现在有所有历史数据

要求 得出 所有股票 的 今天最高值 最低值 以及 这些值出现在什么时候
以及 开盘价 停盘加 开盘价 和停盘的时间


就是一模一样的

作者: im2web   发布时间: 2011-12-14

id 是股票的代号 

打字错误

作者: im2web   发布时间: 2011-12-14

前面做过一个股票最高价最低价的查询.你找找看.

作者: qianjin036a   发布时间: 2011-12-14

参考:
SQL code
select convert(varchar(10),pushtime,120)时间,code as 代码,
(select top 1 price from tb b where code=a.code and convert(varchar(10),pushtime,120)=convert(varchar(10),a.pushtime,120) and not exists(select 1 from tb where code=a.code and convert(varchar(10),pushtime,120)=convert(varchar(10),a.pushtime,120) and pushtime<b.pushtime)) as 开盘价,
(select top 1 price from tb b where code=a.code and convert(varchar(10),pushtime,120)=convert(varchar(10),a.pushtime,120) and not exists(select 1 from tb where code=a.code and convert(varchar(10),pushtime,120)=convert(varchar(10),a.pushtime,120) and pushtime>b.pushtime)) as 收盘价,
(select max(price) from tb where code=a.code and convert(varchar(10),pushtime,120)=convert(varchar(10),a.pushtime,120)) as 最高价,
(select min(price) from tb where code=a.code and convert(varchar(10),pushtime,120)=convert(varchar(10),a.pushtime,120)) as 最低价
from tb a

作者: qianjin036a   发布时间: 2011-12-14