The following SQL statement produces the results listed below. Question is, how do I modify this statement to provide the sum total for each post_id? For example, post_id 1 should = 25, and post_id 2 should = 758.
SELECT t.post_id,
SUM(maxpostmetric) as sumvalue
FROM (
SELECT post_metrics.post_id,
post_metrics.post_metric_type_id,
MAX( post_metrics.value ) maxpostmetric
FROM post_metrics
INNER JOIN posts ON posts.id = post_metrics.post_id
WHERE posts.channel_id = 2268
GROUP BY post_metrics.post_id, post_metrics.post_metric_type_id
ORDER BY post_metrics.id
)t
INNER JOIN post_metric_types ON post_metric_types.id = t.post_metric_type_id
GROUP BY t.post_id, t.post_metric_type_id
post_id:value;
1:0; 1:25; 1:0; 2:110; 2:588; 2:60;
1 = 25; 2 = 758.
Thanks
It’s because you’re grouping by post_metric_type_id as well. What is the purpose of the join and the grouping towards that field? If you want your results to include that field, then add it to your select statement.
However, to get the SUM of the post_id’s, remove that from your GROUP BY clause and you should be good to go.
You may be able to remove your INNER JOIN as well — depends on your needs.
Good luck.