问题:从基因阅读的第一个日期开始,直到会员被取消为止的每月成员数量。
会员每月可以阅读多次。他们可以继续拥有他们想要的尽可能多的读数。
示例:
member_id date gene_a_measurement_done gene_b_measurement_done
5557153 1/1/2010 y
5557153 2/1/2010 y
222458 2/1/2010 y y
222458 1/1/2011 y
707222 1/1/2011 y
另一张表有成员取消日期:
member_id status date
5557153 Cancelled 5/1/2011
222458 Cancelled 12/1/9999
707222 Cancelled 12/1/9999
预期结果:
month distinct_count_of_member_with_gene_a_measurement distinct_count_of_member_with_gene_b_measurement
1/1/10 1 0
2/1/10 2 2
3/1/10 2 2
4/1/10 2 2
5/1/10 1 1
6/1/10 1 1
7/1/10 1 1
8/1/10 1 1
9/1/10 1 1
10/1/10 1 1
11/1/10 1 1
12/1/10 1 1
1/1/11 2 1
查询已尝试:
SELECT
sub.last_day,
sum(sub.distinct_count_of_member_with_gene_a_measurement) as distinct_count_of_member_with_gene_a_measurement,
sum(sub.distinct_count_of_member_with_gene_b_measurement) as distinct_count_of_member_with_gene_b_measurement,
FROM
(SELECT last_day(date),
COUNT(DISTINCT member_id) as distinct_count_of_member_with_gene_a_measurement,
null as distinct_count_of_member_with_gene_b_measurement,
FROM measurement
WHERE gene_a_measurement_done is not null
GROUP BY last_day(date)
UNION ALL
SELECT last_day(date),
null as distinct_count_of_member_with_gene_a_measurement,
COUNT(DISTINCT member_id) as distinct_count_of_member_with_gene_b_measurement,
FROM measurement
WHERE gene_b_measurement_done is not null
GROUP BY last_day(date)) as sub
GROUP BY sub.last_day(date)
以上查询仅提供了完成测量的月份的不同成员计数,我不确定如何最好地考虑取消日期? (在member_id上使用member_status表进行内部联接并且有条件过滤掉已取消的成员?)