Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle SQL - getting a filtered total along with a total overall

Posted on 2014-03-14
4
Medium Priority
?
475 Views
Last Modified: 2014-03-14
I have 3 tables in Oracle 12:

TABLE:Product_Sales
SalesmanID
Product_ID
Sales_Amt

TABLE: Salesman_INFO
SalesmanID
salesman_type


TABLE:Products
Product_ID
product_type


I want to see, for a each sales person type, what % of the sales (by count)
is from a product_type that begins with 'BLUE'.
I use the query below to determine the total of 'BLUE' for each sales person type, but I dont
know how to incorporate the total sales of all irregardless of 'BLUE' so I can calc the percentage:
(Sales of 'Blue')/(Sales of All)

I would like to do it in one SQL if possible.
Any ideas?


Here's the query for the total of 'BLUE%' :

SELECT
B.Salesman_type
,COUNT(DISTINCT A.SalesmanID) AS Salesman
,median(a.NumSales) as Median_Sales
,count(*) as Num_Sales
,round((sum(A.NumSales)/count(*)),0) as Avg_Num_Sales
,sum(A.Sales_Amt) as Sales_Amt


from Product_Sales a
left join Salesman_INFO b on A.SalesmanID = B.SalesmanID
WHERE
Product_ID in
(select Product_ID from Products
where product_type LIKE 'BLUE%')
AND b.Salesman_type IS NOT NULL
GROUP BY B.Salesman_type
ORDER BY sum(a.Sales_Amt) DESC;
0
Comment
Question by:GNOVAK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39929104
what about:
SELECT
B.Salesman_type
,COUNT(DISTINCT A.SalesmanID) AS Salesman
,median(a.NumSales) as Median_Sales
,count(*) as Num_Sales
,round((sum(A.NumSales)/count(*)),0) as Avg_Num_Sales
,sum(A.Sales_Amt) as Sales_Amt

,sum(case when a.product_type Like 'BLUE%' then 1 else  0 end) as Num_Sales_Blue
,round((sum(  case when a.product_type Like 'BLUE%' then A.Sales_Amt else  0 end  )/ sum(case when a.product_type Like 'BLUE%' then 1 else  0 end)  ),0) as Avg_Num_Sales_Blue
,sum(case when a.product_type Like 'BLUE%' then A.Sales_Amt else 0 end) as Sales_Amt_Blue

from Product_Sales a
left join Salesman_INFO b on A.SalesmanID = B.SalesmanID
AND b.Salesman_type IS NOT NULL

JOIN Products p
  ON p.product_id = a.product_id 

GROUP BY B.Salesman_type
ORDER BY sum(a.Sales_Amt) DESC; 

Open in new window

0
 

Author Closing Comment

by:GNOVAK
ID: 39929435
Thanks!
0
 

Author Comment

by:GNOVAK
ID: 39929473
Question -
I just found out that the Products table has more than one product_id. There is a Last_change_date field in addition to the product_id, product_type.

How do I change the (Products P) join to get the latest Last_change_date for ech product_id?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39929538
JOIN Products p
  ON p.product_id = a.product_id
 AND p.last_change_date = ( SELECT MAX (x.last_change_date) FROM Products x
              WHERE x.product_id = a.product_id  )
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question