Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there an alternative to the MULTISET operator that avoids sub-queries?
    primarykey
    data
    text
    <p>Suppose you have a table as follows:</p> <pre><code>CREATE TABLE EMPLOYEE_SALES ( EMPLOYEE_ID NUMBER, PRODUCT_ID NUMBER, SALE_AMOUNT NUMBER ); </code></pre> <p>And suppose it is populated as follows:</p> <pre> +-------------+------------+-------------+ | EMPLOYEE_ID | PRODUCT_ID | SALE_AMOUNT | +-------------+------------+-------------+ | 1 | 100 | 1.05 | | 1 | 200 | 45.67 | | 2 | 100 | 3.25 | | 2 | 200 | 34.29 | +-------------+------------+-------------+ </pre> <p>Now, suppose I create a custom type named <code>SALE_TYPE</code> which represents a <code>(PRODUCT_ID, SALE_AMOUNT)</code> tuple:</p> <pre><code>CREATE TYPE SALE_TYPE IS OBJECT ( PRODUCT_ID NUMBER, SALE_AMOUNT NUMBER ); </code></pre> <p>And suppose I also create a custom type named <code>SALES_TYPE</code> which represents a <code>TABLE</code> of <code>SALE_TYPE</code>:</p> <pre><code>CREATE TYPE SALES_TYPE IS TABLE OF SALE_TYPE; </code></pre> <p>I want to query the <code>EMPLOYEE_SALES</code> table group by <code>EMPLOYEE_ID</code>. For each <code>EMPLOYEE_ID</code>, I wish to create a <code>SALES_TYPE</code> containing that employee's sales. According to the documentation, I would do something like this:</p> <pre><code>SELECT EMPLOYEE_ID, CAST ( MULTISET ( SELECT PRODUCT_ID, SALE_AMOUNT FROM EMPLOYEE_SALES inner_employee_sales WHERE inner_employee_sales.employee_id = employee_sales.employee_id ) AS SALES_TYPE ) AS SALES FROM EMPLOYEE_SALES GROUP BY EMPLOYEE_ID </code></pre> <p>I expect this query to yield something like this:</p> <pre> +-------------+------------------------------+ | EMPLOYEE_ID | SALES | +-------------+------------------------------+ | 1 | +------------+-------------+ | | | | PRODUCT_ID | SALE_AMOUNT | | | | +------------+-------------+ | | | | 100 | 1.05 | | | | | 200 | 45.67 | | | | +------------+-------------+ | +-------------+------------------------------+ | 2 | +------------+-------------+ | | | | PRODUCT_ID | SALE_AMOUNT | | | | +------------+-------------+ | | | | 100 | 3.25 | | | | | 200 | 34.29 | | | | +------------+-------------+ | +-------------+------------------------------+ </pre> <p>Is there a way to achieve the same results without issuing a sub-query (the <em>real</em> query I am working on is much more complicated than this contrived example)? For example, is there something like this:</p> <pre><code>SELECT EMPLOYEE_ID, CAST ( COLLECT_ALL_RECORDS_WITHIN_THE_CURRENT_GROUP( PRODUCT_ID, SALE_AMOUNT ) AS SALES_TYPE ) AS SALES FROM EMPLOYEE_SALES GROUP BY EMPLOYEE_ID </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload