Write select statement so that it will work in sqlite or postgres - but this is probably not the way to do it