一个挺简单的问题,如何进行统计
时间:2011-12-14
来源:互联网
有 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
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
/*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
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 = xx 不是单个的么?
单个id 可以手动run 4个sql 然后把结果合并起来。
我要的是group by id 的做法
作者: im2web 发布时间: 2011-12-14
作者: gogodiy 发布时间: 2011-12-14
`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
打字错误
作者: 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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28