Note that there are some explanatory texts on larger screens.

plurals
  1. PONested query - looking for better solution
    primarykey
    data
    text
    <p>Consider a sales application where we have two tables SALES and INTERNAL_SALES.</p> <ul> <li>Table "SALES" references the number of transactions made by each sales person outside the company.</li> <li>Table "INTERNAL_SALES" references the number of transactions made by each sales person inside the company to another sales person.</li> </ul> <p><strong>SALES Table</strong>: Each date has one entry against each sales person even if transactions are zero.</p> <pre> id | day | sales_person | number_of_transactions 1 | 2011-08-01 | Tom | 1000 2 | 2011-08-01 | Ben | 500 3 | 2011-08-01 | Anne | 1500 4 | 2011-08-02 | Tom | 0 5 | 2011-08-02 | Ben | 800 6 | 2011-08-02 | Anne | 900 7 | 2011-08-03 | Tom | 3000 8 | 2011-08-03 | Ben | 0 9 | 2011-08-03 | Anne | 40 </pre> <p><strong>INTERNAL_SALES Table</strong>: This table logs only the transactions that were actually made between sales persons.</p> <pre> id | day | sales_person_from | sales_person_to | number_of_transactions 0 | 2011-08-01 | Tom | Ben | 10 1 | 2011-08-01 | Tom | Anne | 20 2 | 2011-08-01 | Ben | Tom | 50 3 | 2011-08-03 | Anne | Tom | 30 4 | 2011-08-03 | Anne | Tom | 30 </pre> <p>Now the problem is to come up with total transactions by each sales person on a daily basis. The way I did this is:</p> <pre> SELECT day, sales_person, sum(num_transactions) from ( SELECT day, sales_person, number_of_transactions As num_transactions FROM sales; UNION SELECT day, sales_person_from As sales_person, sum(number_of_transactions) As num_transactions FROM internal_sales GROUP BY day, sales_person_from; ) GROUP BY day, sales_person; </pre> <p>This is too slow and looks ugly. I am seeking a better solution. By the way the database being used in Oracle and I have no control over database except that I can run queries against it.</p>
    singulars
    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.
 

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