使用多个层次级别对值进行排序

时间:2016-11-03 15:43:45

标签: sql oracle sorting hierarchy

我有一个分层产品价格列表,其价格已汇总。层级级别为群集商店货架

Unsorted products prices

我想按层次排序这些产品价格。每个级别都需要进行排序。以下是预期结果的示例:

Expected result

现在,我可以在货架级别对价格进行排序,但不能在商店或集群级别进行排序:

SELECT cluster_id, store_id, shelf_id, price FROM (
    SELECT * FROM (
        SELECT p.*,
            ROW_NUMBER () OVER (
                ORDER BY
                cluster_id ASC NULLS FIRST,
                store_id ASC NULLS FIRST,
                price DESC
            ) AS rn
        FROM (SELECT * FROM products) p
    )
)
ORDER BY rn;

我的实际结果是:

My actual result

我正在使用Oracle Database 12c。

示例数据

CREATE TABLE products (
    cluster_id     VARCHAR2(30),
    store_id       VARCHAR2(30),
    shelf_id       VARCHAR2(40),
    price          NUMBER(*,2)
);

INSERT INTO products VALUES ('10','230',NULL,120);
INSERT INTO products VALUES (NULL,NULL,NULL,500);
INSERT INTO products VALUES ('10','230','967',40);
INSERT INTO products VALUES ('10',NULL,NULL,300);
INSERT INTO products VALUES ('50','430','863',50);
INSERT INTO products VALUES ('50','170',NULL,70);
INSERT INTO products VALUES ('10','500','783',100);
INSERT INTO products VALUES ('50','170','798',20);
INSERT INTO products VALUES ('50','480',NULL,80);
INSERT INTO products VALUES ('50','430',NULL,50);
INSERT INTO products VALUES ('10','500',NULL,180);
INSERT INTO products VALUES ('50','480','486',60);
INSERT INTO products VALUES ('10','230','296',80);
INSERT INTO products VALUES ('50',NULL,NULL,200);
INSERT INTO products VALUES ('10','500','344',80);
INSERT INTO products VALUES ('50','480','234',20);
INSERT INTO products VALUES ('50','170','368',50);

2 个答案:

答案 0 :(得分:0)

我认为这会奏效。

    <div class="field-panel">
        <div>
          <span>
             <input #newData [(ngModel)]="data.id" type="text" >
             <select #newCategory [(ngModel)]="i.index" class="input-bars">
              <option *ngFor="let data of field" [value]="data.value">{{data.value}}</option>
            </select>
          </span>
        <button class="btn btn-success (click)="addData(newData.value, newCategory.value)">ADD</button>
         </div>
    </div>

    addData(value, dropValue){

        this.Data[index] = value;
    }    

答案 1 :(得分:0)

这种排序方法提供了所需的输出:

select p.*
  from products p
  order by 
       sum(case when store_id is not null and shelf_id is not null then price end) 
           over (partition by cluster_id) desc,
       sum(case when store_id is not null and shelf_id is not null then price end) 
           over (partition by cluster_id, store_id) desc,
       case when shelf_id is null then 1 end, price desc