添加一个排名字段和 return 标准的前 3 个数据库
Add a Ranking field and return top 3 data base on Criteria
感谢您调查我的问题。
我希望它达到 return 排名,并根据 "Date"、"Group"、"Sell Count" 进行排名,因此它可以给出前 3 名的排名。例如2014 年 1 月 2 日,John 在 "BIGGroup1A" 中售出 100 和 10。因此,产品 10 中的排名为 1,产品 7 中的排名为 2。同一天,2014 年 1 月 2 日,他售出了 55 和 55两个不同的组,所以它们都应该是 1。我填写了排名的其余部分以符合我追求的逻辑。
我的表是
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
我想要的结果是:
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 1
1/7/2014 John MediumGroup2A Product3 55 1
感谢 sql 中的解决方案,我是新手,希望大家能提供帮助。
您可以使用RANK()
来实现您想要的。
Select *,
RANK() OVER (PARTITION BY DATE,GROUP ORDER BY SELLCOUNT)AS RANK
from Table T
PARTITION BY
根据DATE
对结果集进行分组,GROUP.ORDER BY
确保在创建的分组中根据SELLCOUNT
进行排名。
您应该可以像这样使用 rank()
window 函数:
select * from (
select
*,
rank = rank() over (partition by date, [group] order by date, [group], sellcount desc)
from table1
) t
where rank <= 3
内部查询根据您的指定为每一行分配一个排名,外部查询过滤排名 > 3 的行。
参见 this SQL Fiddle 示例。
感谢您调查我的问题。
我希望它达到 return 排名,并根据 "Date"、"Group"、"Sell Count" 进行排名,因此它可以给出前 3 名的排名。例如2014 年 1 月 2 日,John 在 "BIGGroup1A" 中售出 100 和 10。因此,产品 10 中的排名为 1,产品 7 中的排名为 2。同一天,2014 年 1 月 2 日,他售出了 55 和 55两个不同的组,所以它们都应该是 1。我填写了排名的其余部分以符合我追求的逻辑。
我的表是
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
我想要的结果是:
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 1
1/7/2014 John MediumGroup2A Product3 55 1
感谢 sql 中的解决方案,我是新手,希望大家能提供帮助。
您可以使用RANK()
来实现您想要的。
Select *,
RANK() OVER (PARTITION BY DATE,GROUP ORDER BY SELLCOUNT)AS RANK
from Table T
PARTITION BY
根据DATE
对结果集进行分组,GROUP.ORDER BY
确保在创建的分组中根据SELLCOUNT
进行排名。
您应该可以像这样使用 rank()
window 函数:
select * from (
select
*,
rank = rank() over (partition by date, [group] order by date, [group], sellcount desc)
from table1
) t
where rank <= 3
内部查询根据您的指定为每一行分配一个排名,外部查询过滤排名 > 3 的行。
参见 this SQL Fiddle 示例。