2015-02-06 26 views
1

您能否帮我在sql中获得答案?我想让我当前的表格返回一个“Rank”字段,该字段显示每天基于“Sell Count”的前三名产品。添加排名字段并返回前3个数据

我的表

Date Name ProductName SellCount 
1/2/2014 John Product1 55 
1/2/2014 John Product4 55 
1/2/2014 John Product7 10 
1/2/2014 John Product10 100 
1/3/2014 John Product2 55 
1/3/2014 John Product5 77 
1/3/2014 John Product8 25 
1/3/2014 John Product11 50 
1/4/2014 John Product3 55 
1/4/2014 John Product6 5 
1/4/2014 John Product9 44 
1/4/2014 John Product12 660 

我希望它返回的 “等级” 字段。所以我可以看到每天销售的前三款产品。另外,如果有两个相等的销售计数(例如“排名”字段日期“1/2/2014”):我希望它自动分配一个排名为2和另一个为3.

Date Name ProductName SellCount Rank 
1/2/2014 John Product1 55 3 
1/2/2014 John Product4 55 2 
1/2/2014 John Product10 100 1 
1/3/2014 John Product11 50 3 
1/3/2014 John Product2 55 2 
1/3/2014 John Product5 77 1 
1/4/2014 John Product9 44 3 
1/4/2014 John Product3 55 2 
1/4/2014 John Product12 660 1 

我希望我的问题是不够清楚。如果你们需要我详细说明,请告诉我。我很感谢SQL格式的解决方案。谢谢大家!

第2问: 家伙如果我添加另一列呼叫组

我的新表

 Date Name Group   ProductName SellCount 
1/2/2014 John BigGroup1A Product7 10 
1/2/2014 John BigGroup1A Product10 100 
1/2/2014 John BigGroup1B Product2 55 
1/2/2014 John Group1A   Product1 55 
1/3/2014 John Group1B   Product6 5 
1/3/2014 John Group1C   Product9 44 
1/3/2014 John Group1C   Product4 55 
1/3/2014 John LargeGroup1A Product5 77 
1/4/2014 John LargeGroup2A Product8 25 
1/5/2014 John LargeGroup2B Product12 660 
1/6/2014 John MediumGroup2A Product11 50 
1/7/2014 John MediumGroup2A Product3 55 

(我加了一组名为新列,我想它返回的排名和根据“日期”,“集团”,“卖出计数”,它可以给排名前3名。例如在1/2/2014约翰在“BIGGroup1A”售出100和10.因此排名将是1在产品10中排名第二,在产品7中排名第二。在2014年第二季度的第二天,他在两个不同的组别中卖出了55和55,所以他们都应该是1.我填补了剩下的排名以匹配逻辑im追求。

Date Name Group   ProductName SellCount Rank 
1/2/2014 John BigGroup1A Product7 10   2 
1/2/2014 John BigGroup1A Product10 100   1 
1/2/2014 John BigGroup1B Product2 55   1 
1/2/2014 John Group1A   Product1 55   1 
1/3/2014 John Group1B   Product6 5   1 
1/3/2014 John Group1C   Product9 44   2 
1/3/2014 John Group1C   Product4 55   1 
1/3/2014 John LargeGroup1A Product5 77   1 
1/4/2014 John LargeGroup2A Product8 25   1 
1/5/2014 John LargeGroup2B Product12 660   1 
1/6/2014 John MediumGroup2A Product11 50   2 
1/7/2014 John MediumGroup2A Product3 55   1 

再次感谢!帮助我找到第二部分的解决方案。

+0

我加入部分2中的问题。谢谢大家的帮助 – Nguyenal07 2015-02-06 17:26:26

回答

2

使用窗函数与CTE共同为这种类型的问题:

CREATE TABLE #Temp(
    [Date]  DATE, 
    Name  VARCHAR(20), 
    ProductName VARCHAR(20), 
    SellCount INT 
) 
INSERT INTO #Temp VALUES 
('1/2/2014', 'John', 'Product1', 55), ('1/2/2014', 'John', 'Product4', 55), ('1/2/2014', 'John', 'Product7', 10), 
('1/2/2014', 'John', 'Product10', 100), ('1/3/2014', 'John', 'Product2', 55), ('1/3/2014', 'John', 'Product5', 77), 
('1/3/2014', 'John', 'Product8', 25), ('1/3/2014', 'John', 'Product11', 50), ('1/4/2014', 'John', 'Product3', 55), 
('1/4/2014', 'John', 'Product6', 5), ('1/4/2014', 'John', 'Product9', 44), ('1/4/2014', 'John', 'Product12', 660); 

--Start of the solution 
;WITH CTE AS(
    SELECT 
     *, 
     [Rank] = ROW_NUMBER() OVER(PARTITION BY Name, [Date] ORDER BY SellCount DESC) 
    FROM #Temp --Replace this with your Table 
) 
SELECT * 
FROM CTE 
WHERE [Rank] <= 3 
ORDER BY [Date], [Rank] DESC 
--End of the solution  

DROP TABLE #Temp 

结果

Date  Name     ProductName   SellCount Rank 
---------- -------------------- -------------------- ----------- -------------------- 
2014-01-02 John     Product4    55   3 
2014-01-02 John     Product1    55   2 
2014-01-02 John     Product10   100   1 
2014-01-03 John     Product11   50   3 
2014-01-03 John     Product2    55   2 
2014-01-03 John     Product5    77   1 
2014-01-04 John     Product9    44   3 
2014-01-04 John     Product3    55   2 
2014-01-04 John     Product12   660   1 
+0

嗨Wewsthmeance,我是SQL新手;该表已经创建并且其名为“MyTable”,你可以提供给我SQL,因此我可以将它放在sql server studio中以测试排名字段是否有效。谢谢 – Nguyenal07 2015-02-06 02:33:28

+0

哦,我刚刚为你创建了一些示例数据。只需用'MyTable'替换CTE内部的'#Temp'即可。查看带有评论的更新答案。 – 2015-02-06 02:35:16

+0

谢谢!欣赏它! – Nguyenal07 2015-02-06 06:32:05

0

看看在SQL服务器这些窗口的功能,我相信一个会满足您的需求。

DENSE_RANK - 返回的结果集的分区内的行的等级,而不会在排名的任何间隙。一排的排名是一个加上所讨论的排之前的不同排名的数量。

RANK - 返回结果集的分区内每行的排名。一行的排名是一个加上有问题的行之前的行列数。

- 返回的行的顺序号的结果集的一个分区内,从1开始的每个分区中的第一行。

Other Sql Server Ranking Functions

相关问题