SQL Server按组排名()

JNM*_*JNM 7 sql sql-server reporting-services

我有一个这样的数据表:

Employee1   Product1    ProductGroup1   Quantity    SalesDate
Employee1   Product1    ProductGroup1   Quantity    SalesDate
Employee1   Product2    ProductGroup1   Quantity    SalesDate
Employee1   Product2    ProductGroup1   Quantity    SalesDate
Employee2   Product1    ProductGroup1   Quantity    SalesDate
Employee2   Product1    ProductGroup1   Quantity    SalesDate
Employee2   Product1    ProductGroup1   Quantity    SalesDate
Employee2   Product1    ProductGroup1   Quantity    SalesDate
Employee2   Product2    ProductGroup1   Quantity    SalesDate
Employee2   Product2    ProductGroup1   Quantity    SalesDate
Run Code Online (Sandbox Code Playgroud)

有多个员工,多个产品,多个产品组,多个销售日期.在报告服务中我有一个矩阵,其中父组是员工,子组是产品,列组是销售日期.我需要对产品进行排名,以获得前5名并将其他名单列入其他名单.问题是,我必须在员工组内对产品进行排名,产品可以有多个销售日期,而我需要评估所有内容.在SQL现在我有:Rank() Over (partition by DataTable.ProductGroup1, DataTable.Employee Order by Sum(Quantity) desc) as Rank 但这给了我错误的结果,因为相同的产品具有不同的等级值,因为等级功能在不同的销售日期使用数量排名.我该怎么写sql,所以它返回所有销售日期的数据,使用从所有日期汇总的数量的公交车排名?

编辑:
一些数据集来解释我得到什么和我需要什么.

//DATA I HAVE
Employee_col    Product_col ProductGroup_col    Quantity_col    SalesDate_col
Employee1       Product1    ProductGroup1       100             2012-01
Employee1       Product1    ProductGroup1       200             2012-02
Employee1       Product2    ProductGroup1       50              2012-01
Employee1       Product2    ProductGroup1       80              2012-02
Employee2       Product1    ProductGroup1       200             2012-01
Employee2       Product1    ProductGroup1       70              2012-02
Employee2       Product2    ProductGroup1       20              2012-01
Employee2       Product2    ProductGroup1       450             2012-02

//RESULT I GET
Employee_col    Product_col ProductGroup_col    Quantity_col    SalesDate_col   Rank_col
Employee1       Product1    ProductGroup1       100             2012-01         2
Employee1       Product1    ProductGroup1       200             2012-02         1
Employee1       Product2    ProductGroup1       50              2012-01         4
Employee1       Product2    ProductGroup1       80              2012-02         3
Employee2       Product1    ProductGroup1       200             2012-01         2
Employee2       Product1    ProductGroup1       70              2012-02         3
Employee2       Product2    ProductGroup1       20              2012-01         4
Employee2       Product2    ProductGroup1       450             2012-02         1

//RESULT I NEED
Employee_col    Product_col ProductGroup_col    Quantity_col    SalesDate_col   Rank_col
Employee1       Product1    ProductGroup1       100             2012-01         1
Employee1       Product1    ProductGroup1       200             2012-02         1
Employee1       Product2    ProductGroup1       50              2012-01         2
Employee1       Product2    ProductGroup1       80              2012-02         2
Employee2       Product1    ProductGroup1       200             2012-01         2
Employee2       Product1    ProductGroup1       70              2012-02         2
Employee2       Product2    ProductGroup1       20              2012-01         1
Employee2       Product2    ProductGroup1       450             2012-02         1
Run Code Online (Sandbox Code Playgroud)

pod*_*ska 6

试试这个查询

select
#t.*, salesrank
from #t
inner join 
(
     select Employee, Product, RANK() over (partition by employee order by sq desc) as salesrank
     from
     (select Employee, Product , SUM (Quantity) sq from #t group by Employee, product) v
) v 
    on #t.product = v.product
    and #t.Employee =v.Employee
Run Code Online (Sandbox Code Playgroud)


小智 5

超过RANK()(按Employee_col,Product_col,SalesDate_col订单按Quantity_col ASC进行分区)

  • 您是否阅读过OP关于使用rank()进行尝试的评论?如果是,则应详细说明并解释为什么您的答案很好。 (6认同)
  • 这是**PARTITION BY** 的一个很好的用法! (2认同)