六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

sql语句例子一

[复制链接]

升级  90%

142

主题

142

主题

142

主题

举人

Rank: 3Rank: 3

积分
470
 楼主| 发表于 2013-1-27 04:44:34 | 显示全部楼层 |阅读模式
declare @v_TradingTime datetime
set @v_TradingTime='2008-05-05'

select secucode,   case sum(sign(updownratio))
      when 3 then
       1
      when -3 then
       -1
      else
       0
       end as updown  
                  
         --在这段时间(@v_TradingTime/dateadd(d,-20,@v_TradingTime))内存在的记录条数
     from (select row_number() over(partition by secucode order by tradingtime desc) rn,
         a.*
       from cha_quoteday a
      where markettype in ('A股', 'B股')
        and [close] > 0
        and tradingtime <= @v_TradingTime
        and tradingtime > dateadd(d,-20,@v_TradingTime)
        and exists (select 1 from cha_quoteday c          [email=--@v_TradingTime]--@v_TradingTime[/email]向前推20天内存在的日期的数据
          where a.secucode = c.secucode
           and c.tradingtime [email==@v_TradingTime]=@v_TradingTime[/email]
           and [close] > 0)
               ) b
               where rn<=3
               group by secucode
               having abs(sum(sign(updownratio))) = 3
 
 
2  ROW_NUMBER() OVER 与WITH AS 的用法
WITH orderList AS ( SELECT ROW_NUMBER() OVER (ORDER BY  Articletime DESC)AS Row, *
from Statistics_article
where Articletime between '2008-07-01' and '2008-12-2 0:00:00'  )
SELECT * FROM orderlist WHERE Row between 1 and 20
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表