I am looking at the following materialized view
CREATE MATERIALIZED VIEW ShipStats AS
SELECT country, AVG(displacement), COUNT(*)
FROM classes, ships
WHERE classes.class = ships.name
GROUP BY country;
and trying to figure out what the SELECT [] part of it is doing.
I understand select country. and AVG(displacement) is the average of the displacement column. But what is COUNT(*) doing. Also what would the AVG displacement have to do with a statistic for a single ship?
Relations:
Classes(class, type, country, numGuns, bore, displacement)
Ships(name, class, launched)
count(*)simply returns the amount of rows per group.Because of the
group bystatement all rows of the same country are merged into a single row.count(*)return the amount of rows that go into one such group, whileAVG(displacement)is the average value ofdisplacementover all rows in that group.So the displacement is not for a single ship, but for a group of ships.
If there would be just one row going into such a group,
countwould return 1 andaveragewould return the displacement value of that row.