[英]mysql count number of times a field value appear in another table field
Given that i have 2 tables, how can i see how many distinct values of X is in distinct values of Y but is within 31 days (or a month) before date_X? 给定我有2个表,如何查看date_X之前31天(或一个月)内X的不同值在Y的不同值中?
tb1
date_X X
2015-05-01 cat
2015-05-01 pig
2015-05-01 mouse
2015-04-01 dog
2015-04-01 horse
tb2
date_Y Y
2015-04-30 cat
2015-04-02 cat
2015-04-05 mouse
2015-04-15 rabbit
2015-04-10 pig
2015-03-20 dog
2015-03-10 horse
2015-03-09 frog
For example, i want: 例如,我想要:
date_period num_match count_y percent_match
2015-05-01 2 4 40
2014-04-01 2 3 67
date_period
is unique(date_x) date_period
是唯一的(date_x)
num_match
is the number of distinct(Y) that matches distinct(X) for up to 31 days before given date_period num_match
是在给定date_period之前最多31天与distinct(X)匹配的distinct(Y)的数目
count_y
is the distinct(Y) for up to 31 days before given date_period. count_y
是给定date_period之前最多31天的distinct(Y)。
percent_match
is just num_match
/ count_y
percent_match
只是num_match
/ count_y
This question is an extension to my earlier question here: join mysql on a date range 这个问题是我先前问题的扩展: 在日期范围内加入mysql
One way you can this is with an non-equijoin on date. 一种可行的方法是在日期上使用非等额合并。 Then you can count the distinct values of y either in the set or that match:
然后,您可以计算集合中或匹配项中y的不同值:
select x.date_x,
count(distinct case when x.x = y.y then y.seqnum end) as nummatch,
count(distinct y.seqnum) as count_y,
(count(distinct case when x.x = y.y then y.seqnum end) /
count(distinct y.seqnum)
) as ratio
from x left join
(select y.*, rownum as seqnum
from y
) y
on y.date_y between x.date_x - 31 and x.date_x
group by x.date_x;
EDIT: 编辑:
The above treats the two "cat" lines in y
as being different. 上面将
y
的两条“ cat”行视为不同。 I misread the desired results, so I think the appropriate query is: 我误读了预期的结果,所以我认为适当的查询是:
select x.date_x,
count(distinct case when x.x = y.y then y.y end) as nummatch,
count(distinct y.y) as count_y,
(count(distinct case when x.x = y.y then y.y end) /
count(distinct y.y)
) as ratio
from x left join
y
on y.date_y between x.date_x - 31 and x.date_x
group by x.date_x;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.