基于加权平均的SQL排名

时间:2014-10-22 16:02:18

标签: sql-server

我有两个数据集,我试图根据他们的加权平均排名。我的表1有一个总和,我需要与表2加权平均值进行比较,根据表2中的加权平均值提供排名。表1是主表,但它需要从表2中获得排名提供的加权平均数。问题是两个表没有标识符只有聚合量可用于根据表1中的文件日期从表2中查找排名。基本上,我需要通过table2循环并从表中查找加权平均匹配1,然后分配table2上提供的排名。以下是数据集的示例。

**Table 1**

Categories      File date       Weighted Avg 
Brand A        01/31/2014          89.4
Brand A        02/29/2014          50.1
Brand A        03/31/2014          76.5
Brand A        04/30/2014          75.2
Brand A        05/31/2014          49.2
Brand A        06/30/2014          50.2
Brand A        07/31/2014          76.2
Brand A        08/31/2014          90.1
Brand B        01/31/2014          89.2
Brand B        02/29/2014          50.2
Brand B        03/31/2014          49.3
Brand B        04/30/2014          48.1
Brand B        05/31/2014          89.2
Brand B        06/30/2014          77.71
Brand B        07/31/2014          75.1
Brand B        08/31/2014          74.2

**Table 2**

Ranking    Company            Weighted Avg  
1         Company A                89.30
2         Company B                80.49
3         Company C                77.71
4         Company D                76.3
5         Company D                75.3
6         Company D                74.3
7         Company D                73.3
8         Company D                71.3
9         Company D                68.3
10        Company E                50.86
11        Company E                50.76
12        Company F                49.4
13        Company F                49.3
14        Company G                48.3

**End Results** 

Categories   File date          Weighted Avg    Ranking from table 2
Brand A      01/31/2014           89.4              1
Brand A      02/29/2014           50.1             11
Brand A      03/31/2014           76.5              4
Brand A      04/30/2014           75.2              6
Brand A      05/31/2014           49.2             14
Brand A      06/30/2014           50.2             49.4

我感谢任何帮助?谢谢。

1 个答案:

答案 0 :(得分:0)

如果我没错,这可能对你有所帮助

SELECT a.categories , 
       a.FILEdate , 
       a.weightedavg , 
       b.ranking 
FROM   table1 a 
JOIN   table2 b 
ON     a.weightedavg= b.weightedavg
相关问题