Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用多个分层级别按值排序行_Sql_Oracle_Sorting_Hierarchy - Fatal编程技术网

Sql 使用多个分层级别按值排序行

Sql 使用多个分层级别按值排序行,sql,oracle,sorting,hierarchy,Sql,Oracle,Sorting,Hierarchy,我有一个分级产品价格列表,它们的价格已经汇总。分层级别为集群、商店和货架 我想对这些产品的价格进行分级。每个级别都需要排序。以下是预期结果的示例: 现在,我可以在货架级别对价格进行排序,但还不能在商店或集群级别进行排序: SELECT cluster_id, store_id, shelf_id, price FROM ( SELECT * FROM ( SELECT p.*, ROW_NUMBER () OVER (

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

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

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

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;
我的实际结果是:

我使用的是Oracle数据库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);

我想这会管用的

with t as (
  select cluster_id
  , store_id
  , shelf_id
  , price
  , row_number() over (partition by cluster_id order by price desc) cluster_sort
  , row_number() over (partition by cluster_id, store_id order by price desc) store_sort
  , row_number() over (partition by cluster_id, store_id, shelf_id order by price desc) shelf_sort
)
select cluster_id
, store_id
, shelf_id
, price
from t
order by cluster_sort
, store_sort
, shelf_sort
;

我想这会管用的

with t as (
  select cluster_id
  , store_id
  , shelf_id
  , price
  , row_number() over (partition by cluster_id order by price desc) cluster_sort
  , row_number() over (partition by cluster_id, store_id order by price desc) store_sort
  , row_number() over (partition by cluster_id, store_id, shelf_id order by price desc) shelf_sort
)
select cluster_id
, store_id
, shelf_id
, price
from t
order by cluster_sort
, store_sort
, shelf_sort
;

此排序方法可提供所需的输出:

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

此排序方法可提供所需的输出:

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

您的查询不起作用,它按价格对所有集群进行排序,然后按价格对所有夫妇集群/商店等进行排序。您的查询不起作用,它按价格对所有集群进行排序,然后按价格对所有夫妇集群/商店等进行排序。。。