9

私はこのようなデータテーブルを持っています:

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

複数の従業員、複数の製品、複数の製品グループ、複数の販売日があります。レポートサービスでは、親グループが従業員、子グループが製品、列グループが販売日であるマトリックスがあります。最初の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
4

2 に答える 2

8

このクエリを試してください

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
于 2012-07-17T08:59:59.853 に答える
8

RANK()over(Employee_col、Product_col、SalesDate_colによるPartition_Quantity_col ASCによる注文)

于 2014-03-25T16:12:42.970 に答える