def aggregate_loc_join_1(db, t1, t2, r1, r2):
    # given radius r1, r2
    aggs = ['stddev', 'avg', 'min', 'max', 'count', 'sum']
    t1cols = get_numeric_columns(db, t1, ignore=['id'])
    t2cols = get_numeric_columns(db, t2, ignore=['id'])
    sels1 = ['%s.%s' % (t1, c) for c in t1cols]
    sels2 = ['%s(t2.%s) as %s_%s' % (agg, c, c, agg)
             for c in t2cols for agg in aggs]
    sels = [(t1, c) for c in t1cols] + [(t2, c, agg) for c in t2cols for agg in aggs]
    if not sels:
        return []

    subq = """select %s.id, %s
    from %s, %s as t2
    where circle(%s._latlon, %%s) &&
          circle(%s._latlon, %%s) and
          %s._latlon is not null and
          %s._latlon is not null
    group by %s.id""" % (t1, ','.join(sels2), t1, t2, t1, t2, t1, t2)

    q = """select %s, subq.* from %s, (%s) as subq
    where subq.id = %s.id;""" % (','.join(sels1), t1, subq, t1)
    
    res = db.execute(q, (r1, r2)).fetchall()
    return [dict(zip(sels, row)) for row in res]
dataset/ETHPy150Open sirrice/dbtruck/dbtruck/analyze/fuzzyjoin.py/aggregate_loc_join_1/original