Skip to content

exec

Return selected rows and columns from a table

exec is a qSQL query template and varies from regular q syntax.

For the Exec operator ?, see Functional SQL

Syntax

exec ps [by pb] from texp [where pw]

qSQL syntax

Select phrase

Where the Select phrase

  • is omitted, returns the last record
  • contains a single column, returns the value of that column
  • contains multiple columns or assigns a column name, returns a dictionary with column names as keys
q)\l sp.q

q)exec from sp  / last record
s  | `s!0
p  | `p$`p5
qty| 400

q)exec qty from sp  / list 
300 200 400 200 100 100 300 400 200 200 300 400

q)exec amount:qty from sp  / assigns column name
amount| 300 200 400 200 100 100 300 400 200 200 300 400

q)exec (qty;s) from sp  / list per column 
300 200 400 200 100 100 300 400 200 200 300 400
s1  s1  s1  s1  s4  s1  s2  s2  s3  s4  s4  s1

q)exec qty, s from sp  / dict by column name
qty| 300 200 400 200 100 100 300 400 200 200 300 400
s  | s1  s1  s1  s1  s4  s1  s2  s2  s3  s4  s4  s1

q)exec sum qty by s from sp  / dict by key 
s1| 1600
s2| 700
s3| 200
s4| 600

q)exec q:sum qty by s from sp  / xtab:list!table 
  | q
--| ----
s1| 1600
s2| 700
s3| 200
s4| 600

q)exec sum qty by s:s from sp  / table!list 
s |
--| ----
s1| 1600
s2| 700
s3| 200
s4| 600

q)exec qty, s by 0b from sp  / table
qty s
------
300 s1
200 s1
400 s1
200 s1
100 s4
100 s1
300 s2
400 s2
200 s3
200 s4
300 s4
400 s1

q)exec q:sum qty by s:s from sp
s | q
--| ----
s1| 1600
s2| 700
s3| 200
s4| 600

Compare the results of select and exec queries with multiple columns:

  • a select query result is a table, and all columns are necessarily the same length
  • an exec query result is a dictionary, and column lengths can vary
q)t
name  sex eye
---------------
tom   m   blue
dick  m   green
harry m   blue
jack  m   blue
jill  f   gray
q)select name, distinct eye from t
'length
  [0]  select name, distinct eye from t
       ^
q)exec name, distinct eye from t
name| `tom`dick`harry`jack`jill
eye | `blue`green`gray

Cond

Cond is not supported inside query templates: see qSQL.


delete, select, update
qSQL, Functional SQL
Q for Mortals §9.4 The exec Template