【发布时间】:2019-03-03 20:42:24
【问题描述】:
我有一个包含这些字段的表格:
RefDate
Symbol
Timestamp
Sequence
Quantity
Price
SaleCondition
Pid
SubMkt
它有一个股票交易列表,包括日期(RefDate)、代码(AAPL、MSFT、DAVE、AMZN)、数量、价格等。
SaleCondition 包含我需要用来查找各种东西的代码列表,例如"O" 代表“开仓”,"6" 代表“平仓”,以此类推。
我需要计算开盘价、收盘价、最低/最高/平均价格、VWAP(成交量加权平均价)、交易数量、交易股票数量和波动率(计算为最高价格-最低价/最后价)。
我必须排除 SaleCondition 中的许多代码才能获得 min/max/avg/VWAP,我做对了。
但我不知道如何将开盘价和收盘价拉入查询。我基本上需要给出一个计算字段条件(Select PRICE where SaleCondition="O" and PID="Q")。显然我不能这样做,因为我需要WHERE 子句来排除许多其他代码。
这是我想出的。第一个产生正确的最小/最大/平均/vwap,但开盘/收盘价是占位符,股票/交易不正确。后两个查询是正确的开盘价和收盘价。
dbGetQuery(nqdb, statement =
"select
RefDate,
Symbol,
Price as OpeningPrice,
Price as ClosingPrice,
Min(Price) as MinPrice,
Max(Price) as MaxPrice,
AVG(Price) as AvgPrice,
Sum(Quantity*Price)/Sum(Quantity) as VWAP,
Count(Quantity) as Trades,
Sum(Quantity) as Shares,
(Max(Price)-Min(Price))/(Price) as PctRange
from trds
where
SaleCondition not like '%C%' and
SaleCondition not like '%G%' and
SaleCondition not like '%I%' and SaleCondition not like '%H%' and
SaleCondition not like '%M%' and SaleCondition not like '%N%' and
SaleCondition not like '%P%' and SaleCondition not like '%Q%' and
SaleCondition not like '%R%' and SaleCondition not like '%T%' and
SaleCondition not like '%U%' and SaleCondition not like '%V%' and
SaleCondition not like '%W%' and SaleCondition not like '%Z%' and
SaleCondition not like '%4%' and SaleCondition not like '%7%' and
SaleCondition not like '%9%'
group by Symbol order by PctRange DESC")
dbGetQuery(nqdb, statement =
"select
RefDate,
Symbol,
Price as OpeningPrice
from trds
where SaleCondition like '%O%' and Pid='Q'")
dbGetQuery(nqdb, statement =
"select
RefDate,
Symbol,
Price as ClosingPrice
from trds
where SaleCondition like '%6%' and Pid='Q'")
【问题讨论】:
-
关于每个字符的所有
NOT LIKE。也许您可以使用 NOT GLOB 来简化它,它允许使用字符范围。请注意,GLOB 是区分大小写的。 -
@lukstorms 好看,干杯
标签: sql sqlite calculated-columns