[英]How to get the MAX of a SUM in Oracle
I'm having trouble in getting the person who saw more television for each month, and their respective city. 我很难让每个月看更多电视的人以及他们各自的城市。
I have three tables for this: 我有三个表:
house:
id, street, city
person:
name, id
visualizes:
person, duration, date
So far I have this, but it's only returning the persons who saw the most television of all time: 到目前为止,我有这个功能,但这只是返回有史以来观看电视最多的人:
SELECT TO_CHAR (V.date, 'MM.YYYY'), P.name, P.id, C.city, SUM(V.duration)
FROM visualizes V, house C, person P
WHERE (C.id = P.house)
AND (V.person = P.id)
GROUP BY TO_CHAR (V.date, 'MM.YYYY'), P.name, P.id, C.city
HAVING SUM(V.duration) IN ( SELECT MAX( SUM(V.duration))
FROM visualizes V, house C, person P
WHERE (C.id = P.house)
AND (V.person = P.id)
GROUP BY TO_CHAR (V.date, 'MM.YYYY'), P.name, P.id, C.city
);
Use analytic functions for this: 为此使用分析功能:
SELECT pvc.*
FROM (SELECT TO_CHAR(V.date, 'MM.YYYY'), P.name, P.id, C.city,
SUM(V.duration) as cur,
MAX(SUM(v.duration)) OVER (PARTITION BY TO_CHAR(V.date, 'MM.YYYY')) as maxdur
FROM person P JOIN
visualizes V
ON C.id = P.house JOIN
house C
ON V.person = P.id
GROUP BY TO_CHAR(V.date, 'MM.YYYY'), P.name, P.id, C.city
) pvc
WHERE maxdur = dur;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.