postgresql - Remove duplicate column after SQL query -
i have query i'm getting 2 columns of houseid:
how one?
select vehv2pub.houseid, vehv2pub.vehid, vehv2pub.epatmpg, dayv2pub.houseid, dayv2pub.trpmiles vehv2pub, dayv2pub vehv2pub.vehid >= 1 , dayv2pub.trpmiles < 15 , dayv2pub.houseid = vehv2pub.houseid; and also, how average of epatmpg? query return value?
the elegant way use using clause in explicit join condition:
select houseid, v.vehid, v.epatmpg, d.houseid, d.trpmiles vehv2pub v join dayv2pub d using (houseid) v.vehid >= 1 , d.trpmiles < 15; this way, column houseid in result once, if use select *.
usingshorthand notation: takes comma-separated list of column names, joined tables must have in common, , forms join condition specifying equality of each of these pairs of columns. furthermore, output ofjoin usinghas 1 column each of equated pairs of input columns, followed remaining columns each table.
to average epatmpg selected rows:
select avg(v.epatmpg) avg_epatmpg vehv2pub v join dayv2pub d using (houseid) v.vehid >= 1 , d.trpmiles < 15; if there multiple matches in dayv2pub, derived table can hold multiple instances of each row in vehv2pub after join. avg() based on derived table.
Comments
Post a Comment