以下SQL語句會生成下面列出的結果。問題是,我如何修改此聲明以提供每個post_id的總和?例如,post_id 1應該= 25,並且post_id 2應該= 758.SQL:SUM返回結果的MAX值的總和
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
感謝
作爲一個說明,正確格式的代碼應該有四個空格縮進。 – tadman