I'm trying to create a database view which has the result of an aggregate query for a column value (see this post for the aggregate query used), e.g. something like
MESSAGEVIEW
----------------------------------------------
MESSAGEID SENDER PARTCOUNT
----------------------------------------------
1 Tim 2
2 Bridgekeeper 0
I've tried this code for selecting the view data:
SELECT m.MESSAGEID, m.SENDER,
(SELECT COUNT(mp.MESSAGEID)
FROM MESSAGE m LEFT JOIN MESSAGEPART mp
ON mp.MESSAGEID = m.MESSAGEID GROUP BY m.MESSAGEID) AS PARTCOUNT
FROM MESSAGE m;
which returns ORA-01427: single-row subquery returns more than one row
.
I've also tried
SELECT m.MESSAGEID, m.SENDER, COUNT(mp.MESSAGEID) AS PARTCOUNT
FROM MESSAGE m
LEFT JOIN MESSAGEPART mp ON mp.MESSAGEID = m.MESSAGEID
GROUP BY m.MESSAGEID;
Which returns ORA-00979: not a GROUP BY expression
.
How can I get a view which properly shows this information?