I'm quite a newbie with SQL. I'm currently working on an Oracle database and I've created a report that pulls up data depending on the date range parameter.
The code is as follows:
SELECT
DISTINCT C.CUSTOMER_CODE
, MS.SALESMAN_NAME
, SUM(C.REVENUE_AMT) Rev_Amt
FROM
C_REVENUE_ANALYSIS C
, M_CUSTOMER_H MC
, M_SALESMAN MS
WHERE C.COMPANY_CODE = 'W1'
AND C.CUSTOMER_CODE = MC.CUSTOMER_CODE
AND MC.SALESMAN_CODE = MS.SALESMAN_CODE
AND trunc(C.REVENUE_DATE) between to_date(<STARTDATE>,'YYYYMMDD') and to_date(<ENDDATE>,'YYYYMMDD')
AND MS.COMPANY_CODE = '00'
GROUP BY C.CUSTOMER_CODE, MS.SALESMAN_NAME
ORDER BY C.CUSTOMER_CODE, MS.SALESMAN_NAME
The resulting report for a date range from Jan 1st to April 30th is:
+-----------+--------------+--------------+
|Customer |Salesman Name |Revenue Amount|
+-----------+--------------+--------------+
|Customer 1 |Salesman 1 | 5000.00|
+-----------+--------------+--------------+
|Customer 2 |Salesman 1 | 8000.00|
+-----------+--------------+--------------+
|Customer 3 |Salesman 2 | 300.00|
+-----------+--------------+--------------+
|Customer 4 |Salesman 3 | 600.00|
+-----------+--------------+--------------+
|Customer 5 |Salesman 3 | 5000.00|
+-----------+--------------+--------------+
|Customer 6 |Salesman 3 | 8000.00|
+-----------+--------------+--------------+
|Customer 7 |Salesman 4 | 9000.00|
+-----------+--------------+--------------+
|Customer 8 |Salesman 5 | 2000.00|
+-----------+--------------+--------------+
|Customer 9 |Salesman 6 | 1000.00|
+-----------+--------------+--------------+
|Customer10 |Salesman 6 | 5000.00|
+-----------+--------------+--------------+
|Customer11 |Salesman 7 | 6000.00|
+-----------+--------------+--------------+
|Customer12 |Salesman 8 | 8000.00|
+-----------+--------------+--------------+
Now here is where I need your help, please. I need to show a break up of revenues for each Salesman for each month between Jan to April.
So I would like the result to look like this:
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer |Salesman Name |Rev for Jan|Rev for Feb|Rev for Mar|Rev for Apr|Total Rev Amt|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 1 |Salesman 1 | 1000.00| 1000.00| 1000.00| 2000.00| 5000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 2 |Salesman 1 | 2000.00| 2000.00| 2000.00| 2000.00| 8000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 3 |Salesman 2 | 100.00| 0.00| 100.00| 100.00| 300.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 4 |Salesman 3 | 100.00| 200.00| 100.00| 200.00| 600.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 5 |Salesman 3 | 1000.00| 2000.00| 1000.00| 1000.00| 5000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 6 |Salesman 3 | 1000.00| 2000.00| 1000.00| 4000.00| 8000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 7 |Salesman 4 | 2000.00| 2000.00| 3000.00| 2000.00| 9000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 8 |Salesman 5 | 500.00| 400.00| 500.00| 600.00| 2000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer 9 |Salesman 6 | 200.00| 200.00| 200.00| 400.00| 1000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer10 |Salesman 6 | 1000.00| 1000.00| 2000.00| 1000.00| 5000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer11 |Salesman 7 | 2000.00| 2000.00| 1000.00| 1000.00| 6000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
|Customer12 |Salesman 8 | 2000.00| 2000.00| 2000.00| 2000.00| 8000.00|
+-----------+--------------+-----------+-----------+-----------+-----------+-------------+
Unfortunately, since I am a newbie, I don't have rights to create a Stored Procedure to regularly call this data. So I may have to write redundant code.
But the question really is, what would the code be to be able to break down revenues per each month per salesman?
Also, the number of columns varies depending on the Date Range. Eg; when Jan to April is selected, I get 4 columns for revenue plus 1 column for Total revenue. When Previous year's October to this year April is selected, I get 7 columns for revenue plus 1 column for Total revenue.
PLEASE can someone help a newbie eager to learn and prove himself? I would greatly appreciate your help. Thanks a lot in advance.
EDIT :
After a little persuasion, my manager has agreed to submit my stored procedure for approval and if approved, it will be created.
If I do get to create a stored procedure, what will be the code to get the desired result?
Thanks in advance.