ClickHouse ships with a command line tool which does this (without the actual database server):
ps aux | tail -n +2 | awk '{ printf("%s\t%s\n", $1, $4) }' | \
clickhouse-local -S "user String, mem Float64" \
-q "SELECT user, round(sum(mem), 2) as memTotal FROM table GROUP BY user ORDER BY memTotal DESC FORMAT Pretty"
┏━━━━━━━━━━┳━━━━━━━━━━┓
┃ user ┃ memTotal ┃
┡━━━━━━━━━━╇━━━━━━━━━━┩
│ clickho+ │ 0.7 │
├──────────┼──────────┤
│ root │ 0.2 │
├──────────┼──────────┤
│ netdata │ 0.1 │
├──────────┼──────────┤
│ ntp │ 0 │
├──────────┼──────────┤
│ dbus │ 0 │
├──────────┼──────────┤
│ nginx │ 0 │
├──────────┼──────────┤
│ polkitd │ 0 │
├──────────┼──────────┤
│ nscd │ 0 │
├──────────┼──────────┤
│ postfix │ 0 │
└──────────┴──────────┘
Has the advantage of being really fast.the additional requirement to set up the schema is kind of onerous.
Shameless plug: Sqawk can do nearly the same without you defining a schema.
$ ps aux | sqawk -output table \
'select user, round(sum("%mem"), 2) as memtotal
from a
group by user
order by memtotal desc' \
header=1
┌────────┬────┐
│dbohdan │67.1│
├────────┼────┤
│ root │3.5 │
├────────┼────┤
│ avahi │0.0 │
├────────┼────┤
│ daemon │0.0 │
├────────┼────┤
│message+│0.0 │
├────────┼────┤
│ nobody │0.0 │
├────────┼────┤
│ ntp │0.0 │
├────────┼────┤
│ rtkit │0.0 │
├────────┼────┤
│ syslog │0.0 │
├────────┼────┤
│ uuidd │0.0 │
├────────┼────┤
│whoopsie│0.0 │
└────────┴────┘
Link: https://github.com/dbohdan/sqawk