表 公司
公司名称 股票代码
A公司 100001
B公司 100002表 总股
股票代码 总股数 时间
100001 100 2007-05-01
100002 200 2007-04-26
100001 150 2007-05-20
100002 210 2007-05-30
100001 200 2007-06-29
100002 220 2007-07-10
100001 210 2007-07-11
100002 200 2007-07-11
表 行情 -- 当天收盘价格为负数表示该天停盘,国家假期所有股票停盘
股票代码 当天收盘价格 时间
100001 3.5 2007-04-30
100002 3.8 2007-04-30
100001 -10.0 2007-05-08
100002 4.0 2007-05-08
………………………………………………
100002 4.3 2007-06-07
100001 5.6 2007-06-08
100002 -3.5 2007-06-08
100001 5.8 2007-07-05
100002 6.3 2007-07-05求sql语句 得到某一天的市值 就是不超过这一天的最新的总股数 和 不超过这一天最新的不为负的股票价格的 乘积。例如:6月10日(周日)股票 的市值为 股票代码 单价 总股数 市值
100001 5.6 150 840
100002 4.3 210 903例如 7月10日(周二) 股票的市值为股票代码 单价 总股数 市值
100001 5.8 200 1160
100002 6.3 220 1386
公司名称 股票代码
A公司 100001
B公司 100002表 总股
股票代码 总股数 时间
100001 100 2007-05-01
100002 200 2007-04-26
100001 150 2007-05-20
100002 210 2007-05-30
100001 200 2007-06-29
100002 220 2007-07-10
100001 210 2007-07-11
100002 200 2007-07-11
表 行情 -- 当天收盘价格为负数表示该天停盘,国家假期所有股票停盘
股票代码 当天收盘价格 时间
100001 3.5 2007-04-30
100002 3.8 2007-04-30
100001 -10.0 2007-05-08
100002 4.0 2007-05-08
………………………………………………
100002 4.3 2007-06-07
100001 5.6 2007-06-08
100002 -3.5 2007-06-08
100001 5.8 2007-07-05
100002 6.3 2007-07-05求sql语句 得到某一天的市值 就是不超过这一天的最新的总股数 和 不超过这一天最新的不为负的股票价格的 乘积。例如:6月10日(周日)股票 的市值为 股票代码 单价 总股数 市值
100001 5.6 150 840
100002 4.3 210 903例如 7月10日(周二) 股票的市值为股票代码 单价 总股数 市值
100001 5.8 200 1160
100002 6.3 220 1386
(select 股票代码,总股数,max(时间) 时间
from 总股
where 时间<='2007-06-10'
group by 股票代码,总股数) a,
(select 股票代码,当天收盘价格,max(时间) 时间
from 行情
where 时间<='2007-06-10' and 当天收盘价格>0
group by 股票代码,当天收盘价格) b
where a.股票代码=b.股票代码
--创建测试环境
create table 总股(股票代码 char(6),总股数 int,时间 datetime)
insert into 总股
select '100001', 100, '2007-05-01' union all
select '100002', 200, '2007-04-26' union all
select '100001', 150, '2007-05-20' union all
select '100002', 210, '2007-05-30' union all
select '100001', 200, '2007-06-29' union all
select '100002', 220, '2007-07-10' union all
select '100001', 210, '2007-07-11' union all
select '100002', 200, '2007-07-11'*/
-- 当天收盘价格为负数表示该天停盘,国家假期所有股票停盘
create table 行情(股票代码 char(6), 当天收盘价格 dec(10,2) , 时间 datetime)
insert into 行情
select '100001', 3.5, '2007-04-30' union all
select '100002', 3.8, '2007-04-30' union all
select '100001', -10.0, '2007-05-08' union all
select '100002', 4.0, '2007-05-08' union all
select '100002', 4.3, '2007-06-07' union all
select '100001', 5.6, '2007-06-08' union all
select '100002', -3.5, '2007-06-08' union all
select '100001', 5.8, '2007-07-05' union all
select '100002', 6.3, '2007-07-05'
--写SQL
select a.股票代码,c.当天收盘价格 as 单价, d.总股数,d.总股数*c.当天收盘价格 as 市值
from
(select 股票代码,max(时间) 时间
from 总股
where 时间<='2007-07-10'
group by 股票代码) a,
(select 股票代码,max(时间) 时间
from 行情
where 时间<='2007-07-10' and 当天收盘价格>0
group by 股票代码) b,行情 c,总股 d
where a.股票代码=b.股票代码 and d.股票代码=a.股票代码 and d.时间=a.时间
and c.股票代码=b.股票代码 and c.时间=b.时间