Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:28:15+00:00 2026-05-24T18:28:15+00:00

Consider a sales application where we have two tables SALES and INTERNAL_SALES. Table SALES

  • 0

Consider a sales application where we have two tables SALES and INTERNAL_SALES.

  • Table “SALES” references the number of transactions made by each sales person outside the company.
  • Table “INTERNAL_SALES” references the number of transactions made by each sales person inside the company to another sales person.

SALES Table:
Each date has one entry against each sales person even if transactions are zero.

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

INTERNAL_SALES Table:
This table logs only the transactions that were actually made between sales persons.

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

Now the problem is to come up with total transactions by each sales person on a daily basis. The way I did this is:

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;

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:28:16+00:00Added an answer on May 24, 2026 at 6:28 pm

    There is no need to aggregate twice, and the union operator typically does an implicit unique sort which, again, in not necessary in your case.

    SELECT day, sales_person, sum(num_transactions) from
    (
        SELECT day, sales_person, number_of_transactions As num_transactions FROM sales;
        UNION ALL 
        SELECT day, sales_person_from, number_of_transactions FROM internal_sales;
    )
    GROUP BY day, sales_person;
    

    Removing the intermediate aggregation and the unique sort should help a bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

well i have a table my boss has made... they are 2 tables.. first
Consider the following query where aggregation happens across two tables: Sales and Promo and
Consider the following two EXPLAINs: EXPLAIN SELECT * FROM sales WHERE title != 'The'
consider table sales (id, seller_id, amount, date) and here is a view that is
Consider we have a database that has a table, which is a record of
I have an application that uses several configuration files (let just consider appli.properties here).
Consider a sales department that sets a sales goal for each day. The total
I have a View in my MVC 3 application that lets the user enter
Consider these two functions: 'Overload 1 <Extension()> Function ComputeReturnValue(Of TSource, TResult)(ByVal source As TSource,
Consider an application that stores inventory for a store, which consists of hundreds of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.