添加排名字段和 return 前 3 数据
Add a ranking field and return top 3 data
你能帮我在sql中找到答案吗?我想让我当前的 table return 成为一个 "Rank" 字段,每天根据 "Sell Count" 显示前 3 名产品。
我的Table
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
我想要 Return "Rank" 字段。所以我可以看到每天销量最高的 3 种产品。此外,如果有两个销售计数彼此相等(例如 "rank" 字段日期“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 部分问题:
伙计们,如果我添加另一个 Column 调用组怎么办
我的新 table 是
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
(我添加了一个名为组的新列,我想让它return排名并让它基于"Date"、"Group"、"Sell Count"所以它可以给出前 3 名的排名。例如,在 2014 年 1 月 2 日,John 在 "BIGGroup1A" 中售出 100 件和 10 件。因此产品 10 中的排名将是 1,产品 7 中的排名将是 2。同一天 1/2 /2014,他在两个不同的组中卖出了 55 和 55,所以它们都应该是 1。我填写了排名的其余部分以符合我的逻辑。
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
再次感谢!帮助我找到第二部分的解决方案。
看看 Sql 服务器中的这些窗口函数 我相信其中一个会满足您的需要。
DENSE_RANK - Returns 结果集分区内行的排名,排名没有任何差距。一行的排名是一加上相关行之前的不同排名的数量。
RANK - Returns 结果集分区内每一行的排名。一行的排名是一加上相关行之前的排名数。
ROW_NUMBER - Returns 结果集分区内行的序号,每个分区中的第一行从 1 开始。
将窗口函数与 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
你能帮我在sql中找到答案吗?我想让我当前的 table return 成为一个 "Rank" 字段,每天根据 "Sell Count" 显示前 3 名产品。
我的Table
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
我想要 Return "Rank" 字段。所以我可以看到每天销量最高的 3 种产品。此外,如果有两个销售计数彼此相等(例如 "rank" 字段日期“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 部分问题: 伙计们,如果我添加另一个 Column 调用组怎么办
我的新 table 是
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
(我添加了一个名为组的新列,我想让它return排名并让它基于"Date"、"Group"、"Sell Count"所以它可以给出前 3 名的排名。例如,在 2014 年 1 月 2 日,John 在 "BIGGroup1A" 中售出 100 件和 10 件。因此产品 10 中的排名将是 1,产品 7 中的排名将是 2。同一天 1/2 /2014,他在两个不同的组中卖出了 55 和 55,所以它们都应该是 1。我填写了排名的其余部分以符合我的逻辑。
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
再次感谢!帮助我找到第二部分的解决方案。
看看 Sql 服务器中的这些窗口函数 我相信其中一个会满足您的需要。
DENSE_RANK - Returns 结果集分区内行的排名,排名没有任何差距。一行的排名是一加上相关行之前的不同排名的数量。
RANK - Returns 结果集分区内每一行的排名。一行的排名是一加上相关行之前的排名数。
ROW_NUMBER - Returns 结果集分区内行的序号,每个分区中的第一行从 1 开始。
将窗口函数与 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