Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用sql查找重复率_Sql - Fatal编程技术网

使用sql查找重复率

使用sql查找重复率,sql,Sql,我有一张如下所述的表格: 如果X位客户在1月份进行了购买,他们中有多少人在2月份也进行了购买,即Y重复率:Y/X*100 customer_no month --------------------- 1 jan 2 jan 3 jan 4 jan 11 jan 1 feb 2 feb 3 feb 9 feb 10

我有一张如下所述的表格: 如果X位客户在1月份进行了购买,他们中有多少人在2月份也进行了购买,即Y重复率:Y/X*100

customer_no month
---------------------
1           jan
2           jan
3           jan
4           jan
11          jan
1           feb
2           feb
3           feb
9           feb
10          feb
输出:

 Repeat_Rate
 60%
我想这样做:

   SELECT CAST(COUNT(yourtable_feb.customer_no) as FLOAT) 
          / CAST(COUNT(yourtable_jan.customer_no) AS FLOAT) AS Repeating_Rate
     FROM yourtable yourtable_jan
LEFT JOIN yourtable yourtable_feb
       ON yourtable_jan.customer_no = yourtable_feb.customer_no 
      AND yourtable_feb.mymonth = 'feb'
    WHERE yourtable_jan.mymonth = 'jan'
这里是一个测试人员,如果您想重新测试我的查询:

您能解释一下它是如何工作的吗。理解起来有点困难,只需使用以下语句进行尝试-您可能会更好地理解结果:选择xyz_jan.*,xyz_feb.*从xyz xyz_jan左起,在xyz_jan.customer_no=xyz_feb.customer_no和xyz_feb.mymonth='feb'上加入xyz xyz xyz_feb,其中xyz_jan.mymonth='jan'yourtable_jan包含一月的所有数据。然后,我对所有记录进行左连接,其中mymonth='feb'。在您的示例中,由于客户id 4和11不在2月份:他们在2月份没有购买,因此他们将没有yourtable\u feb记录-最后,我只需将2月份的所有客户编号除以1月份的客户编号