Search code examples
sqloracle-databasesubqueryanalyticsscalar-subquery

oracle scalar subquery?


I have such a table:

 name value1 value2 value3
 ---------------------------
 name1 1       1       1
 name2 1       1       2
 name3 2       2       11
 name4 2      12       2
 name5 3       3       8
 name6 3       3       2

what I need is such a result:

 name value1 value2 value3
 ---------------------------
 name2 1       1       2
 name4 2      12       2
 name5 3       3       8

I.e:

  1. Exact 1 enty for each group of value1;
  2. This entry must have the max value of value2 in this group of value1;
  3. This entry must have the max value of value3 in the group of value2.

After having searched in Internet, I have got a solution by using scalar subquery in SELECT list as a single column, but it is very ugly and complex, since the same sub query must be run for each of the column value1, value2, value3.

SQL Cookbook mentions a solution in recipe 14.10 via defining a type as object, but i prefer a solution in a single SELECT statement.

Any easy way?


Solution

  • Analytics are your friend in this case:

    SQL> CREATE TABLE t (NAME VARCHAR2(32), v1 INTEGER, v2 INTEGER, v3 INTEGER);
    
    Table created
    SQL> INSERT INTO t VALUES ('name1',1,1,1);
    
    1 row inserted
    SQL> INSERT INTO t VALUES ('name2',1,1,2);
    
    1 row inserted
    SQL> INSERT INTO t VALUES ('name3',2,2,11);
    
    1 row inserted
    SQL> INSERT INTO t VALUES ('name4',2,12,2);
    
    1 row inserted
    SQL> INSERT INTO t VALUES ('name5',3,3,8);
    
    1 row inserted
    SQL> INSERT INTO t VALUES ('name6',3,3,2);
    
    1 row inserted
    SQL> SELECT NAME, v1, v2, v3
           FROM (SELECT NAME, v1, v2, v3
                      , MAX(v2) OVER(PARTITION BY v1) mv2
                      , MAX(v3) OVER(PARTITION BY v1,v2) mv3
                   FROM t)
          WHERE v2 = mv2
            AND v3 = mv3
          ORDER BY v1;
    
    NAME   V1 V2 V3
    ------ -- -- --
    name2   1  1  2
    name4   2 12  2
    name5   3  3  8