1

我花了很长时间研究这个问题,但没有发现任何直接解决这个问题的东西。

我试图从总体上查看哪些客户“像”其他客户。

对于另一列的某些值,如何计算一列上重复值的数量?

在下面的示例中,我想知道“客户 112”和“客户 113”有多少记录与“客户 111”显示的变量值重复。

答案是:Customer 112 = 3 (27, 28 and 30 are all duplicates of values shown for Customer 111)Customer 113 = 2 (24 and 26 are both duplicates of values shown for Customer 111)

Customer Variable 
111      21
111      22
111      23
111      24
111      26
111      27
111      28
111      29
111      30
112      23
112      27
112      28
112      30
112      31
112      33
112      35
113      24
113      26
113      33
113      35

输出将是:

Customer  Count
112        3
113        2

任何建议将不胜感激。

4

2 回答 2

3

这是一种方法,通过加入“111”客户价值然后聚合:

select t.customer, count(t111.variable) as "count"
from t left outer join
     (select t.*
      from t
      where customer = 111
     ) t111
     on t.variable = t111.variable
group by t.customer;

我认为上面的内容很清楚它在做什么。但是,您可以消除子查询(这在 MySQL 中很好):

select t.customer, count(t111.variable) as "count"
from t left outer join
     t t111
     on t.variable = t111.variable and t111.customer = 111
group by t.customer;
于 2013-07-30T03:01:47.177 回答
1

这会给你:

Customer  Count
112        3
113        2

这是代码:

SELECT customer,count(variable) 
FROM t where variable in 
   (select variable from t where customer=111)
GROUP BY customer
HAVING customer!=111;

http://www.sqlfiddle.com/#!2/3b3bc/15中查看

于 2013-07-30T03:22:11.350 回答