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 *.

per documentation:

using shorthand 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 of join using has 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

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -