
- KDB+ 教程
- KDB+ - 首頁
- Q 程式語言
- Q 程式語言
- Q 語言 - 型別轉換
- Q 語言 - 時間資料
- Q 語言 - 列表
- Q 語言 - 索引
- Q 語言 - 字典
- Q 語言 - 表格
- Q 語言 - 動詞 & 副詞
- Q 語言 - 連線
- Q 語言 - 函式
- Q 語言 - 內建函式
- Q 語言 - 查詢
- Q - 程序間通訊
- Q - 訊息處理程式(.Z 庫)
- KDB+ 有用資源
- KDB+ - 快速指南
- KDB+ - 有用資源
- KDB+ - 討論
Q 語言 - 查詢
在q 中的查詢更短、更簡單,並且擴充套件了 SQL 的功能。主要的查詢表示式是“選擇表示式”,它在最簡單的形式下提取子表,但它也可以建立新的列。
選擇表示式的一般形式如下:
Select columns by columns from table where conditions
**注意 - by & where 短語是可選的,只有“from 表示式”是必須的。
一般來說,語法將是:
select [a] [by b] from t [where c] update [a] [by b] from t [where c]
q 表示式的語法看起來與 SQL 非常相似,但q 表示式簡單而強大。上面q 表示式的等效 SQL 表示式如下:
select [b] [a] from t [where c] [group by b order by b] update t set [a] [where c]
所有子句都在列上執行,因此q 可以利用順序。由於 Sql 查詢不基於順序,因此它們無法利用該優勢。
與相應的 SQL 相比,q 關係查詢的大小通常要小得多。有序和函式式查詢可以完成 SQL 中難以完成的事情。
在歷史資料庫中,where 子句的排序非常重要,因為它會影響查詢的效能。partition 變數(日期/月份/日期)始終放在首位,然後是排序和索引的列(通常是 sym 列)。
例如,
select from table where date in d, sym in s
比以下速度快得多:
select from table where sym in s, date in d
基本查詢
讓我們在記事本中編寫一個查詢指令碼(如下所示),儲存(為 *.q),然後載入它。
sym:asc`AIG`CITI`CSCO`IBM`MSFT; ex:"NASDAQ" dst:`$":c:/q/test/data/"; /database destination @[dst;`sym;:;sym]; n:1000000; trade:([]sym:n?`sym;time:10:30:00.0+til n;price:n?3.3e;size:n?9;ex:n?ex); quote:([]sym:n?`sym;time:10:30:00.0+til n;bid:n?3.3e;ask:n?3.3e;bsize:n?9;asize:n?9;ex:n?ex); {@[;`sym;`p#]`sym xasc x}each`trade`quote; d:2014.08.07 2014.08.08 2014.08.09 2014.08.10 2014.08.11; /Date vector can also be changed by the user dt:{[d;t].[dst;(`$string d;t;`);:;value t]}; d dt/:\:`trade`quote; Note: Once you run this query, two folders .i.e. "test" and "data" will be created under "c:/q/", and date partition data can be seen inside data folder.
帶有約束條件的查詢
* 表示 HDB 查詢
選擇所有 IBM 交易
select from trade where sym in `IBM
*選擇特定日期的所有 IBM 交易
thisday: 2014.08.11 select from trade where date=thisday,sym=`IBM
選擇所有價格 > 100 的 IBM 交易
select from trade where sym=`IBM, price > 100.0
選擇所有價格小於或等於 100 的 IBM 交易
select from trade where sym=`IBM,not price > 100.0
*選擇特定日期上午 10.30 到 10.40 之間的所有 IBM 交易
thisday: 2014.08.11 select from trade where date = thisday, sym = `IBM, time > 10:30:00.000,time < 10:40:00.000
按價格升序選擇所有 IBM 交易
`price xasc select from trade where sym =`IBM
*在特定時間範圍內按價格降序選擇所有 IBM 交易
`price xdesc select from trade where date within 2014.08.07 2014.08.11, sym =`IBM
複合排序 - 按 sym 升序排序,然後按價格降序排序結果
`sym xasc `price xdesc select from trade where date = 2014.08.07,size = 5
選擇所有 IBM 或 MSFT 交易
select from trade where sym in `IBM`MSFT
*在特定時間範圍內計算所有符號的計數(升序)
`numsym xasc select numsym: count i by sym from trade where date within 2014.08.07 2014.08.11
*在特定時間範圍內計算所有符號的計數(降序)
`numsym xdesc select numsym: count i by sym from trade where date within 2014.08.07 2014.08.11
* 在特定時間範圍內,IBM 股票的最大價格是多少,以及何時首次出現?
select time,ask from quote where date within 2014.08.07 2014.08.11, sym =`IBM, ask = exec first ask from select max ask from quote where sym =`IBM
選擇每個 sym 在每小時時段內的最後價格
select last price by hour:time.hh, sym from trade
帶有聚合的查詢
* 計算所有符號的 vwap(成交量加權平均價格)
select vwap:size wavg price by sym from trade
* 統計特定月份的記錄數(以百萬計)
(select trade:1e-6*count i by date.dd from trade where date.month=2014.08m) + select quote:1e-6*count i by date.dd from quote where date.month=2014.08m
* HLOC – 特定月份 CSCO 的每日最高價、最低價、開盤價和收盤價
select high:max price,low:min price,open:first price,close:last price by date.dd from trade where date.month=2014.08m,sym =`CSCO
* 特定月份 CSCO 的每日 Vwap
select vwap:size wavg price by date.dd from trade where date.month = 2014.08m ,sym = `CSCO
* 計算 AIG 價格的每小時平均值、方差和標準差
select mean:avg price, variance:var price, stdDev:dev price by date, hour:time.hh from trade where sym = `AIG
選擇每小時時段內的價格範圍
select range:max[price] – min price by date,sym,hour:time.hh from trade
* 特定月份 CSCO 的每日價差(平均買價-賣價)
select spread:avg bid-ask by date.dd from quote where date.month = 2014.08m, sym = `CSCO
* 特定月份所有 sym 的每日交易價值
select dtv:sum size by date,sym from trade where date.month = 2014.08m
提取 CSCO 的 5 分鐘 vwap
select size wavg price by 5 xbar time.minute from trade where sym = `CSCO
* 提取 CSCO 的 10 分鐘 K 線圖
select high:max price,low:min price,close:last price by date, 10 xbar time.minute from trade where sym = `CSCO
* 查詢 CSCO 在特定日期內價格超過最後價格 100 個基點(100e-4)的時間
select time from trade where date = 2014.08.11,sym = `CSCO,price > 1.01*last price
* 資料庫中最後一天 MSFT 的全天價格和成交量,以 1 分鐘為間隔
select last price,last size by time.minute from trade where date = last date, sym = `MSFT