---input---
from invoices
filter invoice_date >= @1970-01-16
derive {
  transaction_fees = 0.8,
  income = total - transaction_fees
}
filter income > 1
group customer_id (
  aggregate {
    average total,
    sum_income = sum income,
    ct = count total,
  }
)
sort {-sum_income}
take 10
join c=customers (==customer_id)
derive name = f"{c.last_name}, {c.first_name}"
select {
  c.customer_id, name, sum_income
}
derive db_version = s"version()"

---tokens---
'from'        Name.Variable
' '           Text.Whitespace
'invoices'    Name.Variable
'\n'          Text.Whitespace

'filter'      Name.Variable
' '           Text.Whitespace
'invoice_date' Name.Variable
' '           Text.Whitespace
'>='          Operator
' '           Text.Whitespace
'@1970-01-16' Literal.Date
'\n'          Text.Whitespace

'derive'      Name.Variable
' '           Text.Whitespace
'{'           Punctuation
'\n  '        Text.Whitespace
'transaction_fees' Name.Variable
' '           Text.Whitespace
'='           Operator
' '           Text.Whitespace
'0.8'         Literal.Number.Float
','           Punctuation
'\n  '        Text.Whitespace
'income'      Name.Variable
' '           Text.Whitespace
'='           Operator
' '           Text.Whitespace
'total'       Name.Variable
' '           Text.Whitespace
'-'           Operator
' '           Text.Whitespace
'transaction_fees' Name.Variable
'\n'          Text.Whitespace

'}'           Punctuation
'\n'          Text.Whitespace

'filter'      Name.Variable
' '           Text.Whitespace
'income'      Name.Variable
' '           Text.Whitespace
'>'           Operator
' '           Text.Whitespace
'1'           Literal.Number.Integer
'\n'          Text.Whitespace

'group'       Name.Variable
' '           Text.Whitespace
'customer_id' Name.Variable
' '           Text.Whitespace
'('           Punctuation
'\n  '        Text.Whitespace
'aggregate'   Name.Variable
' '           Text.Whitespace
'{'           Punctuation
'\n    '      Text.Whitespace
'average'     Name.Function
' '           Text.Whitespace
'total'       Name.Variable
','           Punctuation
'\n    '      Text.Whitespace
'sum_income'  Name.Variable
' '           Text.Whitespace
'='           Operator
' '           Text.Whitespace
'sum'         Name.Function
' '           Text.Whitespace
'income'      Name.Variable
','           Punctuation
'\n    '      Text.Whitespace
'ct'          Name.Variable
' '           Text.Whitespace
'='           Operator
' '           Text.Whitespace
'count'       Name.Function
' '           Text.Whitespace
'total'       Name.Variable
','           Punctuation
'\n  '        Text.Whitespace
'}'           Punctuation
'\n'          Text.Whitespace

')'           Punctuation
'\n'          Text.Whitespace

'sort'        Name.Variable
' '           Text.Whitespace
'{'           Punctuation
'-'           Operator
'sum_income'  Name.Variable
'}'           Punctuation
'\n'          Text.Whitespace

'take'        Name.Variable
' '           Text.Whitespace
'10'          Literal.Number.Integer
'\n'          Text.Whitespace

'join'        Name.Variable
' '           Text.Whitespace
'c'           Name.Variable
'='           Operator
'customers'   Name.Variable
' '           Text.Whitespace
'('           Punctuation
'=='          Operator
'customer_id' Name.Variable
')'           Punctuation
'\n'          Text.Whitespace

'derive'      Name.Variable
' '           Text.Whitespace
'name'        Name.Variable
' '           Text.Whitespace
'='           Operator
' '           Text.Whitespace
'f'           Literal.String.Affix
'"'           Literal.String.Double
'{'           Literal.String.Interpol
'c'           Name.Variable
'.'           Operator
'last_name'   Name.Variable
'}'           Literal.String.Interpol
', '          Literal.String.Double
'{'           Literal.String.Interpol
'c'           Name.Variable
'.'           Operator
'first_name'  Name.Variable
'}'           Literal.String.Interpol
'"'           Literal.String.Double
'\n'          Text.Whitespace

'select'      Name.Variable
' '           Text.Whitespace
'{'           Punctuation
'\n  '        Text.Whitespace
'c'           Name.Variable
'.'           Operator
'customer_id' Name.Variable
','           Punctuation
' '           Text.Whitespace
'name'        Name.Variable
','           Punctuation
' '           Text.Whitespace
'sum_income'  Name.Variable
'\n'          Text.Whitespace

'}'           Punctuation
'\n'          Text.Whitespace

'derive'      Name.Variable
' '           Text.Whitespace
'db_version'  Name.Variable
' '           Text.Whitespace
'='           Operator
' '           Text.Whitespace
's'           Literal.String.Affix
'"'           Literal.String.Double
'version()'   Literal.String.Double
'"'           Literal.String.Double
'\n'          Text.Whitespace
