inline view: What percentage of these items exist in each bin selected : Subquery Basics « Subquery « Oracle PL / SQL






inline view: What percentage of these items exist in each bin selected

    
SQL>
SQL> create table t
  2  (item number,
  3   bin number,
  4   primary key (bin , item) );

Table created.

SQL>
SQL> insert into t values(2, 34);

1 row created.

SQL> insert into t values(45,34);

1 row created.

SQL> insert into t values(76,34);

1 row created.

SQL> insert into t values(76,35);

1 row created.

SQL> insert into t values(89,35);

1 row created.

SQL> insert into t values(45,35);

1 row created.

SQL> insert into t values(45,36);

1 row created.

SQL>
SQL> select * from t
  2  order by 1,2;

         2         34
        45         34
        45         35
        45         36
        76         34
        76         35
        89         35

7 rows selected.

SQL>
SQL> 
SQL> select bin, count(*), count(*)/cnt
  2  from (
  3         select bin, count(distinct item) over () AS cnt
  4         from t
  5         where item in (2,45,76,89) )
  6  group by bin, cnt
  7  /

        34          3          .75
        35          3          .75
        36          1          .25

3 rows selected.

SQL> drop table t;

Table dropped.

SQL>

   
    
    
    
  








Related examples in the same category

1.Use sub query as a virtual table
2.Compare with data from subquery
3.An example of a nested three-level subquery
4.If an inner query returns a NULL, the outer query also returns NULL
5.Working with multi-column subqueries
6.Use aggregate function in sub query
7.Sub query: 'SELECT 1 FROM dept d'
8.Using the EXISTS and NOT EXISTS operators
9.Writing Single Row Subqueries
10.Compare with the sub query result
11.Subqueries in a HAVING Clause: Uses a subquery in the HAVING clause of the outer query
12.Subqueries in a FROM Clause (Inline Views)
13.Single Row Subqueries May Return a Maximum of One Row
14.Subqueries May Not Contain an ORDER BY Clause
15.Sub query with table join
16.Writing Multiple Column Subqueries with table join
17.Subquery just returns a literal value: improve performance of your query
18.EXISTS typically offers better performance than IN with subqueries
19.NVL() is used to convert null in correlated query
20.Writing Nested Subqueries
21.subqueries in the SELECT column list (New Way)
22.subqueries in the SELECT column list (Old way)
23.Not equals and subquery
24.Greater than average salary
25.Greater than max(salary)
26.Larger than value from subquery
27.Subquery in select statement
28.Subquery in where clause
29.Subqueries That Return Multiple Results
30.Subqueries in the WHERE Clause: equals
31.Subqueries in the WHERE Clause: less than
32.Simple Subqueries in select statement
33.Single-row subqueries return only one row of result.
34.The parent query of a single-row subquery can return more than one row.
35.Born after employee 4 was born