I'm trying to create a SELECT query that does several calculated fields on one of two tables. I'm new to SQL (I've looked at several free online tutorials, so I have a general idea), but I think my goal is a little out of my skill range.
I have two tables:
TreeRecordwith columnsID(serial),Site(chr)Each ID represents an individual tree.
TreeHistorywith columnsID(serial),TreeID(int),DBH(int)DBH is tree diameter.
Currently I can create this:
| Site | Total tree count of site | Avg DBH of site |
I would like to have another column that can give the total count of trees over a particular size for each site. I can recreate this in a simple query, and my research on stack (SQL Select - Calculated Column if Value Exists in another Table) makes me feel that a nested SELECT is what I'm after but I can't get that to work. My current code is this:
SELECT
"TreeRecord"."Site",
count("TreeRecord".*) AS Total_Count,
round(avg("TreeHistory"."DBH"), 0) AS Average_DBH
FROM
"TreeRecord"
LEFT OUTER JOIN
"TreeHistory" ON "TreeRecord"."ID" = "TreeHistory"."TreeID"
GROUP BY
"Site"
ORDER BY
"Site" ASC;
Any help on this would be most appreciated.
Thank you