日期:2014-05-18  浏览次数:20446 次

一个挺简单的问题,如何进行统计
有一个 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))

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



------解决方案--------------------
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

------解决方案--------------------
参考:
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

------解决方案--------------------
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

楼主的想法说的不是很清楚。
你直接告诉我们你想获取的结果是什么信息,直接用文字描述就可以了
------解决方案--------------------
SQL code
CREATE TABLE #temp
(
      id int,
      recvdate datetime,
      value int
)

INSERT INTO #temp(id,recvdate,value)VALUES(1,'2011-12-1',100)
INSERT INTO #temp(id,recvdate,value)VALUES(1,'2011-11-8',50)
INSERT INTO #temp(id,recvdate,value)VALUES(1,'2011-10-21',150)
INSERT INTO #temp(id,recvdate,value)VALUES(2,'2011-09-10',200)
INSERT INTO #temp(id,recvdate,value)VALUES(2,'2011-12-5',10)
INSERT INTO #temp(id,recvdate,value)VALUES(2,'2011-01-5',245)
INSERT INTO #temp(id,recvdate,value)VALUES(3,'2010-12-5',10)
INSERT INTO #temp(id,recvdate,value)VALUES(3,'2011-12-6',100)
INSERT INTO #temp(id,recvdate,value)VALUES(3,'2011-12-7',50)
INSERT INTO #temp(id,recvdate,value)VALUES(4,'2011-12-10',12)



SELECT 
t.id,max(t.value),(SELECT recvdate FROM #temp WHERE t.id=id AND max(t.value)=value)
FROM #temp AS t
GROUP BY id

UNION

SELECT 
t.id,min(t.value),(SELECT recvdate FROM #temp WHERE t.id=id AND min(t.value)=value)
FROM #temp AS t
GROUP BY id

UNION 

SELECT 
t