日期:2014-05-18 浏览次数:20608 次
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
------解决方案--------------------
参考:
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
楼主的想法说的不是很清楚。
你直接告诉我们你想获取的结果是什么信息,直接用文字描述就可以了
------解决方案--------------------
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