首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一SQL (来看看,有合适的答案马上结帖,)

2012-04-04 
求一SQL (高手进来看看,有合适的答案马上结帖,在线等!!)表公司公司名称股票代码A公司100001B公司100002表

求一SQL (高手进来看看,有合适的答案马上结帖,在线等!!)
表   公司
公司名称     股票代码
  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 B.股票代码,B.单价,A.总股数,市值 = B.单价*A.总股数 from 总股 A, 行情 B where A.股票代码 = B.股票代码 and A.時間= B.時間

這個意思?
[解决办法]
不对啊!!要的不是 时间相同的!
要的是不超过某一天的最新的不为负的数据
[解决办法]
得到某一天的市值 就是不超过这一天的最新的总股数 和 不超过这一天最新的不为负的股票价格的 乘积。

什么叫最新?我看你给的基础表中,每条代号每天就一条记录,这样拿来的最新啊?
[解决办法]
喔,看下面

select B.股票代码,單價 = (select top 1 A. 当天收盘价格 from 行情 A where A.股票代码 = B.股票代码 and A.当天收盘价格 > =0 and A.時間 <= B.時間 order by A.時間 desc),B.总股数,市值 = (單價 *B.总股数) from 总股 B

[解决办法]
create table #
(股票代码 varchar(10), 当天收盘价格 dec(18,1), 时间 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 '

create table #t (股票代码 varchar(10), 总股数 int, 时间 datetime)
insert into #t 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 '

select a.股票代码,a.当天收盘价格,b.总股数,(a.当天收盘价格*b.总股数)[市值]
from
(select * from # a where not exists
(select 1 from # b where b.时间 <= '20070610 ' and 当天收盘价格> 0 and b.时间> a.时间 and b.股票代码=a.股票代码)
and 时间 <= '20070610 ' and 当天收盘价格> 0)a
join
(select 股票代码,
(select 总股数 from #t where 时间= max(a.时间))[总股数]
from #t a where 时间 <= '20070610 ' group by 股票代码)b
on b.股票代码=a.股票代码


股票代码 当天收盘价格 总股数 市值
---------- -------------------- ----------- -------------------------------
100001 5.6 150 840.0
100002 4.3 210 903.0

(所影响的行数为 2 行)
[解决办法]
declare @日期 datetime
set @日期 = '2007-06-10 '
select
c.股票代码,
单价 = (
select top 1 总股数 from 总股 where 时间 <= @日期 and 股票代码 = c.股票代码 and 总股数 is not null order by 时间 desc
)*
总股数(
select top 1 当天收盘价格 from 行情 where 时间 <= @日期 and 当前价 > = 0 and 股票代码 = c.股票代码 order by 日期 desc
) ,
单价*总股数 as 市值
from 公司 c order by 市值 desc

热点排行
Bad Request.