Q by example: (q-sql collection)

table create

stock:([sym:`symbol$()] name:`symbol$(); industry:`symbol$())
trade:([] sym:`stock$(); date:`date$(); time:`time$(); quant:`int$();px:`float$())

tuple insert

`stock insert (`0001.HK; `$"Cheung Kong (Holdings) Limited"; `$"Properties & Construction")
`stock insert (`0005.HK; `$"HSBC Holdings plc (Hong Kong)"; `$"Financials")
`stock insert (`0700.HK; `$"Tencent Holdings Ltd"; `$"Internet")
`stock insert (`2800.HK; `$"Tracker Fund of Hong Kong"; `$"ETF")
`stock insert (`2823.HK; `$"iShares FTSE/Xinhua A50 China Trkr (ETF)"; `$"ETF")
`stock insert (`0008.HK; `$"PCCW Limited"; `$"Telecommunications")

tuple delete

delete from `trade where sym=`0001.HK (note here `trade instead of trade)

UDF (user defined functions)

filltrade:{[tname;s;p;n] sc:n#s; dc:2011.01.01+n?31; tc:n?24:00:00.000; qc:10*n?1000; pc:.01*floor (.9*p)+n?.2*p*:100; tname insert (sc;dc;tc;qc;pc) }
filltrade[`trade;`0001.HK;115;10000]
filltrade[`trade;`0005.HK;30;5000]
filltrade[`trade;`0700.HK;540;12000]
filltrade[`trade;`2800.HK;25;4000]
filltrade[`trade;`2823.HK;40;9000]
filltrade[`trade;`0008.HK;35;3000]

sample analytics

`date`time xasc `trade

OLAP-fashion queries

select tot:count sym by sym from trade
select maxp:max px, minp:min px by date from trade where sym=`0700.HK
select vwap:quant wavg px by sym, date from trade where sym in `0001.HK`0700.HK`2800.HK, date within(2011.01.01;2011.01.03)

data import/export between KDB and CSV

save `:trade.txt
table: ("DFFFFIS";enlist ",") 0:`700HK.csv (sample file here)
if the column names are not available in the file:
- tmp: ("DFFFFIS";",") 0:`table.new1
- histprices: flip `date`open`high`low`close`volume`sym!tmp