情况是这样的
表1是所有股票发布公告的日期,
表2是所有股票在6年之内的所有交易记录,日期不连续,按时间顺序排列现在想在表2中提取数据,满足:
如果以表2中最接近表1某支股票公告信息的日期设为0
则提取该日期之前40个和该日期之后9个交易数据
本来如果日期是连续的,用datediff函数很简单,但就是因为交易记录的日期不是连续的
所以不知道应该怎么完成....求助~~~先谢谢了~~很急....

解决方案 »

  1.   

    --假设表2中有唯一IDselect b.*
    from bulletin a inner join trade b
    on a.stockno=b.stockno
    where b.id in
    (
    select top 40 id
    from table2
    where stockno=a.stockno
    and date<a.date
    order by date desc
    )
    union
    select b.*
    from bulletin a inner join trade b
    on a.stockno=b.stockno
    where b.id in
    (
    select top 9 id
    from table2
    where stockno=a.stockno
    and date>a.date
    order by date
    )
      

  2.   

    select * from t_2 c
    where
    c.rq in (
    select top 41 b.rq from t_2 b
            where   b.rq <= ( select a.rq from t_1 a
                               where a.股票名字 = b.股票名字 ) 
    and b.股票名字 = c.股票名字
    order by b.rq desc
             )union allselect * from t_2 c
    where
    c.rq in (
    select top 9 b.rq from t_2 b
            where   b.rq > ( select a.rq from t_1 a
                               where a.股票名字 = b.股票名字 ) 
    and b.股票名字 = c.股票名字
             )