我有一个事件表和一个事务日志。 我想用一个sql计算每个事件的总收入。 什么都可以告诉你如何做到这一点 请知道,交易表中将有超过100,000个日志。
event_table:
Event_id | start_date | end_date
------------------------
11111 | 2013-01-04 | 2013-01-05
11112 | 2013-01-08 | 2013-01-10
11113 | 2013-01-11 | 2013-01-12
11114 | 2013-01-15 | 2013-01-18
11115 | 2013-01-19 | 2013-01-21
11116 | 2013-01-22 | 2013-01-24
11117 | 2013-01-26 | 2013-01-29
transaction_log:
id | name | time_created | Cost
------------------------
1 | michael | 2013-01-04 | 1
2 | michael | 2013-01-08 | 4
3 | mary | 2013-01-11 | 5
4 | john | 2013-01-15 | 2
5 | michael | 2013-01-19 | 3
6 | mary | 2013-01-22 | 2
7 | john | 2013-01-26 | 4
我尝试使用如下的sql,但它不起作用。
select
event_table.id,
( select sum(Cost)
from transaction_log
where date(time_created) between transaction_log.start_date and transaction_log.end_date ) as revenue
from event_table
答案 0 :(得分:0)
它失败了,因为字段start_date
和end_date
来自event_table
,但您将其标记为transaction_log.start_date
和transaction_log.end_date
。这将有效:
select
event_table.id,
( select sum(Cost)
from transaction_log
where date(time_created) between event_table.start_date and event_table.end_date ) as revenue
from event_table
如果time_created
已经是日期数据类型,则无需将date(time_created)
转换为日期(select
event_table.id,
( select sum(Cost)
from transaction_log
where time_created >= event_table.start_date::timestamptz and time_created < (event_table.end_date+1)::timestamptz ) as revenue
from event_table
)。否则,如果time_created是timestamp或timestamptz,那么为了提高性能,您可能需要考虑这样做:
event_table
同样为了提高性能,在执行上述查询时,PostgreSQL正在为主查询的每一行执行子查询(在本例中为select e.id, sum(l.Cost) as revenue
from event_table e
join transaction_log l ON (l.time_created BETWEEN e.start_date AND e.end_date)
group by e.id
表)。加入和使用GROUP BY通常会为您提供更好的结果:
C:/project/some-project