#!/usr/bin/env bash
echo "Searching for $1"
sqlite3 -list -separator '	' ~/.mutt/abook.db << EOSQL
SELECT s.email, s.name, s.frequency
FROM search s
INNER JOIN
    (SELECT email, max(frequency) AS maxfrequency FROM search GROUP BY email) m
ON s.email = m.email AND s.frequency = m.maxfrequency
WHERE s.name
LIKE '%$1%' OR s.email LIKE '%$1%'
ORDER BY s.frequency DESC;
EOSQL
