分组相等值 - 聚合函数问题
grouping equal values - aggregate function problem
我创建了一个 table,其中列出了多个事件发生的地点。同一场地的同一活动可能有不同的价格。最后一列计算一个场所的总收入。
SELECT venuename,
eventname,
totalprice,
SUM(totalprice) OVER (PARTITION BY venuename)
FROM venue
INNER JOIN event e ON venue.venueid = e.venueid
INNER JOIN listing l ON e.eventid = l.eventid;
venue
event
totalprice
sum
Venue A
Event A
5
30
Venue A
Event A
10
30
Venue A
Event B
5
30
Venue A
Event A
5
30
Venue A
Event B
5
30
Venue B
Event C
3
15
Venue B
Event D
6
15
Venue B
Event D
6
15
现在我想将相等的事件分组并计算一个场馆中一个事件的总收入。
结果应如下所示:
venue
event
totalprice
sum
Venue A
Event A
20
30
Venue A
Event B
10
30
Venue B
Event C
3
15
Venue B
Event D
12
15
我解决这个问题的想法是这样的:
SELECT venuename,
eventname,
SUM(totalprice),
SUM(totalprice) OVER (PARTITION BY venuename)
FROM venue
INNER JOIN event e ON venue.venueid = e.venueid
INNER JOIN listing l ON e.eventid = l.eventid
GROUP BY eventname;
但这似乎不可能,因为 vanuename 也必须是聚合函数的一部分。但是我真的想不出解决这个问题的办法。
希望你们中的任何人都可以帮助我。
问候
您可以组合聚合和 window 函数
SELECT venuename,
eventname,
SUM(totalprice) totalprice,
SUM(SUM(totalprice)) OVER (PARTITION BY venuename) sum
FROM venue
INNER JOIN event e ON venue.venueid = e.venueid
INNER JOIN listing l ON e.eventid = l.eventid
GROUP BY venuename, eventname;
我创建了一个 table,其中列出了多个事件发生的地点。同一场地的同一活动可能有不同的价格。最后一列计算一个场所的总收入。
SELECT venuename,
eventname,
totalprice,
SUM(totalprice) OVER (PARTITION BY venuename)
FROM venue
INNER JOIN event e ON venue.venueid = e.venueid
INNER JOIN listing l ON e.eventid = l.eventid;
venue | event | totalprice | sum |
---|---|---|---|
Venue A | Event A | 5 | 30 |
Venue A | Event A | 10 | 30 |
Venue A | Event B | 5 | 30 |
Venue A | Event A | 5 | 30 |
Venue A | Event B | 5 | 30 |
Venue B | Event C | 3 | 15 |
Venue B | Event D | 6 | 15 |
Venue B | Event D | 6 | 15 |
现在我想将相等的事件分组并计算一个场馆中一个事件的总收入。
结果应如下所示:
venue | event | totalprice | sum |
---|---|---|---|
Venue A | Event A | 20 | 30 |
Venue A | Event B | 10 | 30 |
Venue B | Event C | 3 | 15 |
Venue B | Event D | 12 | 15 |
我解决这个问题的想法是这样的:
SELECT venuename,
eventname,
SUM(totalprice),
SUM(totalprice) OVER (PARTITION BY venuename)
FROM venue
INNER JOIN event e ON venue.venueid = e.venueid
INNER JOIN listing l ON e.eventid = l.eventid
GROUP BY eventname;
但这似乎不可能,因为 vanuename 也必须是聚合函数的一部分。但是我真的想不出解决这个问题的办法。
希望你们中的任何人都可以帮助我。
问候
您可以组合聚合和 window 函数
SELECT venuename,
eventname,
SUM(totalprice) totalprice,
SUM(SUM(totalprice)) OVER (PARTITION BY venuename) sum
FROM venue
INNER JOIN event e ON venue.venueid = e.venueid
INNER JOIN listing l ON e.eventid = l.eventid
GROUP BY venuename, eventname;