2

I have this query but I'm getting two columns of houseid:

How do I only get one?

SELECT vehv2pub.houseid, vehv2pub.vehid, vehv2pub.epatmpg, 
       dayv2pub.houseid, dayv2pub.trpmiles
FROM vehv2pub, dayv2pub
WHERE vehv2pub.vehid >= 1
      AND dayv2pub.trpmiles < 15
      AND dayv2pub.houseid = vehv2pub.houseid;

And also, how do I get the average of the epatmpg? So the query would just return the value?

3
  • 1
    The two houseid are from different tables and you get both because you select both. Commented Dec 6, 2014 at 0:15
  • Ok I just realized that. Thank you. How do I take the average of the 'epatmpg' ? So the query would just return the value? Commented Dec 6, 2014 at 0:21
  • just remove one houseid from your select list. Commented Dec 6, 2014 at 0:27

2 Answers 2

6

The most elegant way would be to use the USING clause in an explicit join condition:

SELECT houseid, v.vehid, v.epatmpg, d.houseid, d.trpmiles
FROM   vehv2pub v
JOIN   dayv2pub d USING (houseid)
WHERE  v.vehid >= 1
AND    d.trpmiles < 15;

This way, the column houseid is in the result only once, even if you use SELECT *.

Per documentation:

USING is a shorthand notation: it takes a comma-separated list of column names, which the joined tables must have in common, and forms a join condition specifying equality of each of these pairs of columns. Furthermore, the output of JOIN USING has one column for each of the equated pairs of input columns, followed by the remaining columns from each table.

To get the average epatmpg for the selected rows:

SELECT avg(v.epatmpg) AS avg_epatmpg
FROM   vehv2pub v
JOIN   dayv2pub d USING (houseid)
WHERE  v.vehid >= 1
AND    d.trpmiles < 15;

If there are multiple matches in dayv2pub, the derived table can hold multiple instances of each row in vehv2pub after the join. avg() is based on the derived table.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok I just realized that. Thank you. How do I take the average of the 'epatmpg' ? So the query would just return the value?
You mean one avg over the whole (joined) table?
Yes. But I think I just figured it out. How would YOU do it?
Erwin's answer is excellent. Just one caveat - the above may not get you what you want for average if there are "duplicate" rows in the query, it all depends on the specifics of the data. We'd have to know what is in the vehv2pub table and dayv2pub table to be sure.
@EGP: Exactly. I provided the avg over the whole derived table like requested. I added some explanation.
0

not 100% sure this works in postgres sql, but something like this gets the average in SQL server:

SELECT vehv2pub.houseid, avg(vehv2pub.epatmpg)
FROM vehv2pub, dayv2pub
WHERE vehv2pub.vehid >= 1
AND   dayv2pub.trpmiles < 15
AND   dayv2pub.houseid = vehv2pub.houseid
GROUP BY vehv2pub.houseid

1 Comment

I should note, that is the average per houseid. If you want the average across all houses, just remove the group by and the houseid from the select. In either case, depending on what data is in the tables, it may not wind up with the exact result you want if there are "duplicates." You might have to do a subquery if that's the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.