SQL脚本积累之一-----UNION的使用示例

本博客展示了如何使用SQL查询来筛选特定价格区间内的市场数据,包括成交价低于当日行情最低价、高于最高价及不在五档行情中的交易记录。

查询【成交价】小于当日行情中的显示的【最低价】

SELECT [SpotCode],MinPrice,[Code],LPrice 
FROM (SELECT [SpotCode],Min([TradePrice]) MinPrice,Max([TradePrice]) MaxPrice 
        FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_TodayTradeTable]
        where [TradePrice]>0
        group by SpotCode) tempA,
     (SELECT [Code],Max([High]) HPrice,Min([Low]) LPrice    
        FROM [MRS].[dbo].[SHL1_MarketInfo_20120130]
        where [Low]>0 
        and [High]>0
        group by [Code]
      union 
      SELECT [Code],Max([High]) HPrice,Min([Low]) LPrice    
        FROM [MRS].[dbo].[SZL1_MarketInfo_20120130] 
        where [Low]>0 
        and [High]>0
        group by [Code]) tempB 
where tempA.[SpotCode]=tempB.[Code]
and tempA.MinPrice<tempB.LPrice

查询【成交价】高于当日行情中的显示的【最高价】

SELECT [SpotCode],MaxPrice,[Code],HPrice 
FROM (SELECT [SpotCode],Min([TradePrice]) MinPrice,Max([TradePrice]) MaxPrice 
        FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_TodayTradeTable]
        where [TradePrice]>0
        group by SpotCode) tempA,
     (SELECT [Code],Max([High]) HPrice,Min([Low]) LPrice    
        FROM [MRS].[dbo].[SHL1_MarketInfo_20120130]
        where [Low]>0 
        and [High]>0
        group by [Code]
      union 
      SELECT [Code],Max([High]) HPrice,Min([Low]) LPrice    
        FROM [MRS].[dbo].[SZL1_MarketInfo_20120130] 
        where [Low]>0 
        and [High]>0
        group by [Code]) tempB 
where tempA.[SpotCode]=tempB.[Code]
and tempA.MaxPrice>tempB.HPrice 

查询成交价不在五档行情中

SELECT tempD.[EntrustNumber]
FROM (SELECT  count([EntrustNumber]) num,[EntrustNumber]
        FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_TodayTradeTable] tempA,
             [MRS].[dbo].SZL1_MarketInfo_20120201 tempB
        where tempA.[SpotCode] = tempB.Code
          and tempA.[TradePrice]>0
          and tempB.MarketRefreshTime > dateadd(mi,-10,tempA.TradeTime) 
          and tempB.MarketRefreshTime < dateadd(mi,+10,tempA.TradeTime) 
          and tempA.[BuySellTypeId]=1  
        group by [EntrustNumber]
        union 
     SELECT count([EntrustNumber]) num,[EntrustNumber]
        FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_TodayTradeTable] tempA,
             [MRS].[dbo].SHL1_MarketInfo_20120201 tempC 
          where tempA.[SpotCode]= tempC.Code
          and tempA.[TradePrice]>0
          and tempC.MarketRefreshTime > dateadd(mi,-10,tempA.TradeTime) 
          and tempC.MarketRefreshTime < dateadd(mi,+10,tempA.TradeTime) 
          and tempA.[BuySellTypeId]=1 
        group by [EntrustNumber]) tempD,
    (SELECT  count([EntrustNumber]) num,[EntrustNumber]
            FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_TodayTradeTable] tempA,
                 [MRS].[dbo].SZL1_MarketInfo_20120201 tempB
            where tempA.[SpotCode] = tempB.Code
              and tempA.[TradePrice]>0
              and tempA.[TradePrice] not in(tempB.lastprice,tempB.[SellFirstPrice],tempB.[SellSecondPrice],tempB.[SellThirdPrice],tempB.[SellFourthPrice],tempB.[SellFivePrice])
              and tempB.MarketRefreshTime > dateadd(mi,-10,tempA.TradeTime) 
              and tempB.MarketRefreshTime < dateadd(mi,+10,tempA.TradeTime) 
              and tempA.[BuySellTypeId]=1  
            group by [EntrustNumber]
            union 
         SELECT count([EntrustNumber]) num,[EntrustNumber]
            FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_TodayTradeTable] tempA,
                 [MRS].[dbo].SHL1_MarketInfo_20120201 tempC 
              where tempA.[SpotCode]= tempC.Code
              and tempA.[TradePrice]>0
              and tempA.[TradePrice] not in(tempC.lastprice,tempC.[SellFirstPrice],tempC.[SellSecondPrice],tempC.[SellThirdPrice],tempC.[SellFourthPrice],tempC.[SellFivePrice])
              and tempC.MarketRefreshTime > dateadd(mi,-10,tempA.TradeTime) 
              and tempC.MarketRefreshTime < dateadd(mi,+10,tempA.TradeTime) 
              and tempA.[BuySellTypeId]=1 
            group by [EntrustNumber]) tempE
where tempD.[EntrustNumber]=tempE.[EntrustNumber]
and tempD.num=tempE.num

查询成交价不在五档行情中的SQL语句—-历史成交表与撮合表

SELECT tempD.[EntrustNumber] 
FROM (SELECT  count([EntrustNumber]) num,[EntrustNumber]
        FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_HistoryTradeTable] tempA,
             [VTS_MatchCenter_4.5].[dbo].[XH_MarketInfo] tempB
        where tempA.[SpotCode] = tempB.Code
        and tempA.[TradeTime] > '2012-01-30 18:30:22.920'
        and tempA.[TradePrice]>0
        and tempA.[TradePrice] not in(tempB.[lastprice],tempB.[SellPrice1],tempB.[SellPrice2],tempB.[SellPrice3],tempB.[SellPrice4],tempB.[SellPrice5])
        and tempB.[MarketTime]>dateadd(mi,-5,tempA.TradeTime) 
        and tempB.[MarketTime]<dateadd(mi,+5,tempA.TradeTime) 
        group by [EntrustNumber]) tempC,
    (SELECT count([EntrustNumber]) num,[EntrustNumber]
        FROM [VTS_ReckoningCounter_4.5].[dbo].[XH_HistoryTradeTable] tempA,
             [VTS_MatchCenter_4.5].[dbo].[XH_MarketInfo] tempB
        where tempA.[SpotCode] = tempB.Code
        and tempA.[TradeTime] > '2012-01-30 18:30:22.920'
        and tempA.[TradePrice]>0
        and tempB.[MarketTime]>dateadd(mi,-5,tempA.TradeTime) 
        and tempB.[MarketTime]<dateadd(mi,+5,tempA.TradeTime) 
        group by [EntrustNumber]) tempD
where tempC.[EntrustNumber]=tempD.[EntrustNumber]
and tempC.num = tempD.num
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值