stock sql

select 
  day  统计时间,
  round(avg(rise),2) 平均涨幅,
  count(*) 总数,
  count(case when risepct>0 then 1  END) 涨,
  count(case when risepct=0 then 1  END) 平,
  count(case when risepct<0 then 1  END) 跌,
  count(case when risepct>=9.9 then 1  END)              "涨幅大于9.9%",
  count(case when risepct>=9 and risepct<9.9 then 1  END) "大于9%",
  count(case when risepct>=8 and risepct<9 then 1  END)   "大于8%",
  count(case when risepct>=7 and risepct<8 then 1  END)   "大于7%",
  count(case when risepct>=6 and risepct<7 then 1  END)   "大于6%",
  count(case when risepct>=5 and risepct<6 then 1  END)   "大于5%",
  count(case when risepct>=4 and risepct<5 then 1  END)   "大于4%",
  count(case when risepct>=3 and risepct<4 then 1  END)   "大于3%",
  count(case when risepct>=2 and risepct<3 then 1  END)   "大于2%",
  count(case when risepct>=1 and risepct<2 then 1  END)   "大于1%",
  count(case when risepct>=0 and risepct<1 then 1  END)   "大于0",
  count(case when risepct>=-1 and risepct<0 then 1  END)  "大于负1%",
  count(case when risepct>=-2 and risepct<-1 then 1  END) "大于负2%",
  count(case when risepct>=-3 and risepct<-2 then 1  END) "大于负3%",
  count(case when risepct>=-4 and risepct<-3 then 1  END) "大于负4%",
  count(case when risepct>=-5 and risepct<-4 then 1  END) "大于负5%",
  count(case when risepct>=-6 and risepct<-5 then 1  END) "大于负6%",
  count(case when risepct>=-7 and risepct<-6 then 1  END) "大于负7%",
  count(case when risepct>=-8 and risepct<-7 then 1  END) "大于负8%",
  count(case when risepct>=-9 and risepct<-8 then 1  END) "大于负9%",
  count(case when risepct>=-9.9 and risepct<-9 then 1  END) "大于负9.9%",
  count(case when risepct<-9.9 then 1  END) "涨幅小于负9.9%"
from stockhis t where t.day>'2013-01-01' and substr(t.code,0,1) in ('0','3','6')
group by day
having count(1)>500
order by day desc;

--今日涨幅>9.9%
select '今日涨幅>9.9%' 标题,''''||t.code 代码,t.stockname 名称,t.day 日期,t.price 现价,t.risepct 涨幅,t.exchangepct 换手率
       ,c.groupname 板块
from stockhis t 
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code 
where t.day = (select max(day) from stockhisavg) 
      and t.risepct>9.9
 order by risepct desc
;

--跌停<9.9
select '今日跌幅>-9.9%' 标题,''''||t.code 代码,t.stockname 名称,t.day 日期,t.price 现价,t.risepct 涨幅,t.exchangepct 换手率
       ,c.groupname 板块
from stockhis t 
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code 
where t.day = (select max(day) from stockhisavg) 
      and t.risepct<-9.9
 order by risepct desc
;

--首次涨停 5日内今日涨停
WITH tmp AS
(
SELECT t.* FROM stockhis t 
     LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=5 ) dd ON 1=1
where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min ORDER BY CODE 
)
SELECT '5日内今日涨停' 标题,''''||his.CODE 代码,his.stockname 名称,his.day 日期,pct.totalrisepct 五日涨幅,pct.涨天数,pct.跌天数, his.exchangepct 换手率,c.groupname 板块
FROM 
       (select CODE FROM tmp tt where risepct>9.9 HAVING COUNT(*)=1 GROUP BY CODE ) tt 
  LEFT JOIN (select t.* from stockhis t where t.day = (select max(day) from stockhis) and t.risepct>9.9) his  ON his.code=tt.code--有一个涨停
  LEFT JOIN (SELECT CODE,SUM(risepct) totalrisepct,count(case when risepct>0 then 1  END) 涨天数,count(case when risepct<0 then 1  END) 跌天数 FROM tmp GROUP BY CODE) pct ON pct.code=his.code --总涨幅
  left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl WHERE class='行业分类' group by cl.code) c on c.code=his.code 
  
WHERE 1=1 AND substr(his.code,0,1) in('0','3','6')
;

 --量价升
select '量价升' 标题,''''||t.code 代码,t.stockname 名称,t.day 日期,t.price 现价,t.risepct 涨幅
       ,c.groupname 板块
from stockhis t 
     LEFT JOIN stockhisavg h ON h.code=t.code AND h.day=t.day
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code 
where t.day = (select max(day) from stockhisavg) 
      and t.risepct>5
      AND h.totalvolume>h.totalvolume5*1.05
 order by risepct desc
 ;

--N倍5日均量
select 'N倍5日均量' 标题,''''||t.code 代码,t.stockname 名称,t.day 日期,t.price 现价,h.risepct 涨幅,h.exchangepct 换手率,round(t.totalvolume/t.totalvolume5,2) N倍5日均量
       ,c.groupname 板块
from stockhisavg t left join (select 2 pct from dual) p on 1=1 --设置涨跌幅
     left join stockhis h on h.code=t.code and t.day=h.day
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code
where t.day = (select max(day) from stockhisavg) 
      and t.price>t.avg5 and t.totalvolume>t.totalvolume5*p.pct
      and substr(t.code,0,1) in('0','3','6')
order by 6 desc;


--上下影线
--上影线 5%
SELECT '上影线 5%' 标题,t.code 代码,t.stockname 名称,t.day 日期,t.price 现价,t.risepct 涨幅,t.exchangepct 换手率,
     (case when t.risepct>=0 then round((t.daymax-t.price)/t.price*100,2) else round((t.daymax-t.startprice)/t.startprice*100,2) end) 上影线幅度
     ,c.groupname 板块
FROM stockhis t left join (select 1.05 pct from dual) p on 1=1 --设置涨跌幅
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code 
where 1=1 
      and t.day = (select max(day) from stockhis) 
      and substr(t.code,0,1) in('0','3','6')
      and t.daymax>t.startprice*p.pct 
      and t.daymax>t.price*p.pct
;


--下影线 5%
SELECT '下影线 5%' 标题,t.code 代码,t.stockname 名称,t.day 日期,t.price 现价,t.risepct 涨幅,t.exchangepct 换手率,
     (case when t.risepct>=0 then round((t.startprice-t.daymin)/t.startprice*100,2) else round((t.price-t.daymin)/t.price*100,2) end) 下影线幅度
     ,c.groupname 板块
FROM stockhis t left join (select 1.05 pct from dual) p on 1=1 --设置涨跌幅
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code
where 1=1 
      and t.day = (select max(day) from stockhis) 
      and substr(t.code,0,1) in('0','3','6')
      and t.daymin*p.pct       and t.daymin*p.pct ;

--上下影线 5%
SELECT t.code,t.stockname,t.day,t.price,t.risepct,
     (case when t.risepct>=0 then round((t.daymax-t.price)/t.price,2) else round((t.daymax-t.startprice)/t.startprice,2) end) uppct,
     (case when t.risepct>=0 then round((t.startprice-t.daymin)/t.startprice,2) else round((t.price-t.daymin)/t.price,2) end) downpct
     ,c.groupname
FROM stockhis t left join (select 1.02 pct from dual) p on 1=1 --设置涨跌幅
     left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl where class='行业分类' group by cl.code) c on c.code=t.code
where 1=1 
      and t.day = (select max(day) from stockhis) 
      and substr(t.code,0,1) in('0','3','6')
      and t.daymin*p.pct       and t.daymin*p.pct       and t.daymax>t.startprice*p.pct
      and t.daymax>t.price*p.pct
;

--连涨3天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=3 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>0.5
HAVING COUNT(*)=3
GROUP BY CODE,stockname 
;

--涨停3天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=3 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>9.9
HAVING COUNT(*)=3
GROUP BY CODE,stockname 
;

--连涨4天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=4 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>0.5
HAVING COUNT(*)=4
GROUP BY CODE,stockname 
;

--涨停4天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=4 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>9.9
HAVING COUNT(*)=4
GROUP BY CODE,stockname 
;


--连涨5天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=5 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>0.5
HAVING COUNT(*)=5
GROUP BY CODE,stockname 
;

--涨停5天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=5 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>9.9
HAVING COUNT(*)=5
GROUP BY CODE,stockname 
;

--连涨N天
select code,stockname,SUM(risepct),COUNT(*) FROM(
      SELECT t.*,ROW_NUMBER() OVER(PARTITION BY t.code,t.stockname ORDER BY t.day desc) AS RN   
        FROM stockhis t 
             LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=7 ) dd ON 1=1
        where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min
) tt
where risepct>0
HAVING COUNT(*)=7
GROUP BY CODE,stockname 
;

--5天3涨停
WITH tmp AS
(
SELECT t.* FROM stockhis t 
     LEFT JOIN (SELECT MAX(DAY) MAX,MIN(DAY) MIN FROM (SELECT DISTINCT DAY FROM stockhis  ORDER BY DAY DESC) where rownum <=5 ) dd ON 1=1
where substr(t.code,0,1) in('0','3','6') AND t.day<=dd.max AND t.day >=dd.min ORDER BY CODE 
)
SELECT '5天3涨停' 标题,tt.CODE,tt.stockname,pct.totalrisepct,pct.涨天数,pct.跌天数, tmp.exchangepct,c.groupname
FROM 
  (select CODE,stockname FROM tmp tt where risepct>9.9 HAVING COUNT(*)=3 GROUP BY CODE,stockname ) tt --有3个涨停
  LEFT JOIN (SELECT CODE,SUM(risepct) totalrisepct,count(case when risepct>0 then 1  END) 涨天数,count(case when risepct<0 then 1  END) 跌天数 FROM tmp GROUP BY CODE) pct ON pct.code=tt.code --总涨幅
  LEFT JOIN tmp ON tmp.code=tt.code 
  left join (select cl.code,wmsys.wm_concat(cl.groupname) groupname  from stockclass cl WHERE class='行业分类' group by cl.code) c on c.code=tt.code 
WHERE 1=1 AND substr(tt.code,0,1) in('0','3','6')
      AND tmp.day=(SELECT MAX(DAY) FROM tmp)
;
--换手率前50
SELECT * FROM (SELECT * FROM stockhis t WHERE t.day=(SELECT MAX(DAY) FROM stockhis) AND substr(t.code,0,1) in('0','3','6') ORDER BY t.exchangepct DESC) WHERE ROWNUM<51;
--换手率大于10
SELECT * FROM stockhis t WHERE t.day=(SELECT MAX(DAY) FROM stockhis) AND t.exchangepct>10 AND substr(t.code,0,1) in('0','3','6') ORDER BY t.exchangepct DESC;
--换手率小于2
SELECT * FROM stockhis t WHERE t.day=(SELECT MAX(DAY) FROM stockhis) AND t.exchangepct<2 AND substr(t.code,0,1) in('0','3','6') ORDER BY t.exchangepct DESC;
--成交额小于1亿
SELECT * FROM stockhis t WHERE t.day=(SELECT MAX(DAY) FROM stockhis) AND t.tradingvolume<10000 AND substr(t.code,0,1) in('0','3','6') ORDER BY t.exchangepct DESC;
 

你可能感兴趣的:(数据库,数学建模,sql,股票)