【发布时间】:2014-01-08 08:38:45
【问题描述】:
我想根据以下查询中指定的条件每周汇总过去 3 个月的帐户计数。在以 num_of_accounts 和周为列的表中获取此数据的最有效方法是什么。
select COUNT(DISTINCT a.account_id) as num_accounts,
WEEKOFYEAR(a.ds) as week
FROM
(SELECT
CAST(account_id as BIGINT)
FROM
tableA
WHERE ds='2013-12-28') a
JOIN
tableB b
ON a.account_id=b.account_id AND
b.ds='2013-12-28'
WHERE
b.invoice_date between '2013-12-22' AND '2013-12-28' AND
b.payment_status = 'failed' AND b.payment_status = 'unbilled'
【问题讨论】: