34,590
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,recvdate datetime,value decimal(10,2))
insert into tb select 1,'2011-12-01 09:30:06',12.56
insert into tb select 1,'2011-12-01 11:05:32',12.68
insert into tb select 1,'2011-12-01 11:25:37',12.84
insert into tb select 1,'2011-12-01 13:25:17',12.62
insert into tb select 1,'2011-12-01 15:00:00',12.44
insert into tb select 1,'2011-12-02 11:00:00',12.90
go
select a.id,'2011-12-01' as dt,a.value as 开盘价,b.value as 最高价,c.value as 最低价,d.value as 收盘价
from (
select * from tb a where convert(varchar(10),recvdate,120)='2011-12-01'
and not exists(select 1 from tb where id=a.id and convert(varchar(10),recvdate,120)='2011-12-01' and recvdate<a.recvdate)
)a inner join (
select * from tb a where convert(varchar(10),recvdate,120)='2011-12-01'
and not exists(select 1 from tb where id=a.id and convert(varchar(10),recvdate,120)='2011-12-01' and value>a.value)
)b on a.id=b.id
inner join (
select * from tb a where convert(varchar(10),recvdate,120)='2011-12-01'
and not exists(select 1 from tb where id=a.id and convert(varchar(10),recvdate,120)='2011-12-01' and value<a.value)
)c on c.id=a.id
inner join (
select * from tb a where convert(varchar(10),recvdate,120)='2011-12-01'
and not exists(select 1 from tb where id=a.id and convert(varchar(10),recvdate,120)='2011-12-01' and recvdate>a.recvdate)
)d on d.id=a.id
/*
id dt 开盘价 最高价 最低价 收盘价
----------- ---------- -------------- -------------- -------------- --------------------
1 2011-12-01 12.56 12.84 12.44 12.44
(1 行受影响)
*/
go
drop table tb
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.id,(SELECT value FROM #temp WHERE t.id=id AND max(t.recvdate)=recvdate),max(recvdate)
FROM #temp AS t
GROUP BY id
UNION
SELECT
t.id,(SELECT value FROM #temp WHERE t.id=id AND min(t.recvdate)=recvdate),min(recvdate)
FROM #temp AS t
GROUP BY 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
*
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