Oracle-从列中仅选择最大值

时间:2018-10-28 18:22:03

标签: sql database oracle join select

我的数据库的结构大致是这样(简化):

三个表:顺序,步骤,物料

表“ order”包含列IDmaterial

表“ step”包含列IDorderstepnumber

表“材料”包含列IDdescription

            Order                         
    --------------------------------    
     **ID** **Number**     **Material**    
       1         X1            11
       2         X2            12
       3         X3            13


              Step
   ---------------------------------------
   **ID**    **Order**  **Stepnumber***
     1          X1           X110
     2          X1           X120
     3          X1           X170
     4          X1           X180
     5          X2           X270
     6          X2           X280

                 Material
   ---------------------------------------
  **ID**      **Description***
    11             Mat1
    12             Mat2
    13             Mat3

需要注意的是,表的stepnumber列由订单号(例如X1)和标识步骤(10、20、70、80等)的数字组成,并且您可以看到stepnumber列中有多个值引用相同的顺序。

我正在使用此查询:

select order.number,step.stepnumber, material.id,material.description, 
from db.order inner join db.stepnumber
on order.number = step.order
inner join db.material
on material.id = order.material
where step.stepnumber not like '%10'
and step.stepnumber not like '%20'
group by order.number, step.stepnumber, material.id, material.description

从查询中可以看到,我想提取订单和步骤数据,并且基于stepnumber值排除某些步骤。

现在,查询输出为:

order.number step.stepnumber material.id material.description 
-------------------------------------------------------------
  X1             X170             11           Mat1
  X1             X180             11           Mat1
  X2             X270             12           Mat2
  X2             X280             12           Mat2

我想要实现的是,对于每个订单,仅提取一行,其中stepnumber值较高。所以结果应该是:

order.number step.stepnumber material.id material.description 
-------------------------------------------------------------
  X1             X180             11           Mat1
  X2             X280             12           Mat2

我尝试将MAX函数放到选择中的step.stepnumber上,但没有任何效果。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用ROW_NUMBER窗口功能

CREATE TABLE "Order"(
   ID int,
   "Number" varchar(50),
   Material int
);


INSERT INTO  "Order" VALUES (1,'X1',11);
INSERT INTO  "Order" VALUES (2,'X2',12);
INSERT INTO  "Order" VALUES (3,'X3',13);

CREATE TABLE Step(
   ID int,
   "Order" varchar(50),
   Stepnumber varchar(50)
);


insert into Step values (1,'X1','X110');
insert into Step values (2,'X1','X120');
insert into Step values (3,'X1','X170');
insert into Step values (4,'X1','X180');
insert into Step values (5,'X2','X270');
insert into Step values (6,'X2','X280');

CREATE TABLE Material(
   ID int,
   Description varchar(50)
);               


INSERT INTO Material VALUES (11,'Mat1');
INSERT INTO Material VALUES (12,'Mat2');
INSERT INTO Material VALUES (13,'Mat3');

查询1

SELECT "Number",STEPNUMBER,ID,DESCRIPTION 
FROM (
  select o."Number",
           s.stepnumber, 
           m.id,
           m.description,
           ROW_NUMBER() OVER(PARTITION BY o."Number" ORDER BY s.stepnumber DESC) rn
    from db."Order" o
    inner join db.Step s on o."Number" = s."Order"
    inner join db.material m on m.id = o.material
    where 
        s.stepnumber not like '%10'
    and 
        s.stepnumber not like '%20'
) t1
WHERE rn = 1

Results

| Number | STEPNUMBER | ID | DESCRIPTION |
|--------|------------|----|-------------|
|     X1 |       X180 | 11 |        Mat1 |
|     X2 |       X280 | 12 |        Mat2 |

注意

我们可能会尝试避免使用OrderNumber作为列名或表名...,因为它们是Oracle中的关键字。