答案 0 :(得分:1)
尽管存在大量下挫,但一种方法是使用子查询来限制每个客户的最后日期结束时间。
SELECT
t1.customer_id,
SUM(closing) AS closing_sum
FROM yourTable t1
INNER JOIN
(
SELECT customer_id, MAX(entry_dt) AS max_entry_dt
FROM yourTable
GROUP BY customer_id
) t2
ON t1.customer_id = t2.customer_id AND
t1.entry_dt = t2.max_entry_dt
GROUP BY t1.customer_id
答案 1 :(得分:0)
@tim
我已按如下方式修改了查询。
SELECT
a.customer_id,
SUM(closing) AS closing_sum
FROM empty_accounting a
INNER JOIN
(
SELECT customer_id, MAX(created_dt) AS max_created_dt
FROM empty_accounting
GROUP BY customer_id
) b
ON a.customer_id = b.customer_id AND
a.created_dt = b.max_created_dt
GROUP BY a.customer_id
但我担心如果created_dt在几秒钟内有所不同,那就不会总结。
有没有办法找到最新的created_dt的entry_dt,然后总结每个客户的entry_dt的结束?
注意:entry_dt是事务在现实生活中发生的日期 和created_dt是在系统中创建记录的日期。所以我觉得使用created_dt字段
找到最新记录会更好