您能否帮我在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
再次感谢!帮助我找到第二部分的解决方案。
我加入部分2中的问题。谢谢大家的帮助 – Nguyenal07 2015-02-06 17:26:26