1

我有一张表格,其中包含如下所示的股票订单:

在此处输入图像描述

第一列是相应股票的 ID,第二列是订单价格,第三列是买入(买入)还是卖出(卖出),最后一列是该订单中剩余的交易量。

我想要做的是获得这些值:

  • 最高和最低买入价以及最高和最低卖出价。
  • 最高出价成交量和最低卖出成交量总和

我意识到这是一个非常复杂的查询,它可能会涉及一个(或者可能是几个子查询),但我到目前为止是这样的:

SELECT
  SYMBOL_CODE as symbol_code,
  MAX(ORDER_PRICE) as highest_bid,
  MIN(ORDER_PRICE) as lowest_bid,
  SUM(VOLUME_TRADED)
FROM ORDERS
WHERE
  ORDER_TYPE = 'buy'
GROUP BY
  SYMBOL_CODE
order by
  SYMBOL_CODE;

在我看来,这有两个问题,首先,它汇总了所有价格的数量,而不仅仅是最高出价订单的价格,无法弄清楚如何做到这一点,其次,这只是出价,我'd 必须为询问编写另一个查询。我尝试按 order_type 分组,但没有得到预期结果(股票有 1 行或 2 行结果,具体取决于它们是否有买/卖单或两者兼有)

4

4 回答 4

0

我创建了一个如下视图以保留所有指定的详细信息,然后根据需要从视图中进行选择

订单量

类型

列。

alter view MinMax as
select [SYMBOL_CODE] as Symbol,
        MAX(ORDER_PRICE) as 'Price',
        'Max' as 'Type',
        case max(order_type) when 'buy' then 'BID'  
        else 'ASK'
        end as 'Order Type' ,
        'Order' as 'Order_Volume'
        FROM ORDERS
where ORDER_PRICE in(select MAX(ORDER_PRICE) FROM ORDERS)
group by [SYMBOL_CODE]
union 
select SYMBOL_CODE ,
        sum(VOLUME_TRADED),
        'Max',
        case max(order_type) when 'buy' then 'BID'
        else 'ASK'
        end ,
        'Order'
        FROM ORDERS
where VOLUME_TRADED in(select MAX(VOLUME_TRADED) FROM ORDERS)
group by [SYMBOL_CODE]
union
select SYMBOL_CODE as Symbol,
       MAX(ORDER_PRICE),
       'Max',
       'BID',
        'Volume' 
        FROM ORDERS
where VOLUME_TRADED in(select MAX(VOLUME_TRADED) FROM ORDERS) and order_type='buy'
group by [SYMBOL_CODE]
union
select SYMBOL_CODE,
       MAX(ORDER_PRICE),
        'Min',
        'ASK',
        'Volume'
        FROM ORDERS
where VOLUME_TRADED in(select min(VOLUME_TRADED) FROM ORDERS) and order_type='sell'
group by [SYMBOL_CODE]

如果您还想根据预算类型获得订单,我还添加了一个名为“OrderType”的新列。从视图中选择以获得指定的结果。

Select * from minmax

如果您只想要 SYMBOL_ID 和价格,那么:

select symbol,price from minmax
于 2020-01-09T11:08:17.497 回答
0

您可以使用 筛选最高和最低价格的表格row_number(),然后进行条件聚合:

select
    symbol_code,
    max(case when order_type = 'buy' and rn_high = 1 then order_price end) highest_bid,
    sum(case when order_type = 'buy' and rn_high = 1 then volume_traded end) volume_at_highest_bid,
    min(case when order_type = 'sell' and rn_low = 1 then order_price end) lowest_ask,
    sum(case when order_type = 'sell' and rn_low = 1 then volume_traded end) volume_at_lowest_ask
from (
    select
        o.*,
        row_number() over(partition by symbol_code, order_type order by order_price) rn_low,
        row_number() over(partition by symbol_code, order_type order by order_price desc) rn_high
    from orders o
) t
where 
    order_type = 'buy' and rn_high = 1
    or (order_type = 'sell' and rn_low = 1)
group by symbol_code
于 2020-01-09T09:55:33.133 回答
0

我认为窗口函数和适当的聚合可以满足您的需求:

select symbol_code, max_buy, min_ask,
       sum(case when order_type = 'buy' and order_price = max_buy then volume_traded) as max_buy_volume,
       sum(case when order_type = 'sell' and order_price = min_ask then volume_traded) as min_ask_volume
from (select o.*,
             max(case when order_type = 'buy' then order_price end) over (partition by symbol_code) as max_buy,
             min(case when order_type = 'sell' then order_price end) over (partition by symbol_code) as min_ask
       from orders o
      ) o
group by symbol_code, max_buy, min_ask;
于 2020-01-09T11:52:45.453 回答
0

除了 Partition-- 函数之外,您是否尝试过使用 Min()/Max() 函数?这样您就可以将查询结果“拆分”为每个“符号代码”分区并获得相应的最小值/最大值

因为看起来,你真正想做的是查看某个“符号代码”的所有数据,然后“迭代”它们

于 2020-01-09T11:31:12.480 回答