I am new to SQL. I am currently working on an Oracle database , and I have created a report that retrieves data based on a 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
Final report for the date range from January 1 to April 30:
+-----------+--------------+--------------+ |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 this is where I need your help, please. I need to show the income difference for each Seller for each month between January and April.
Therefore, 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'm new, I don’t have permission to create a stored procedure to call this data regularly. Therefore, I may have to write redundant code.
But in fact, the question is, what would the code be able to divide the income for each month into a seller?
In addition, the number of columns varies depending on the date range. For instance; when Jan to April is selected, I get 4 columns for revenue plus 1 column for total revenue. When the previous year is selected from October to April, I get 7 columns for revenue plus 1 column for total revenue.
PLEASE can someone help beginners who want to learn and prove themselves? I would really appreciate your help. Thank you very much in advance.
EDIT:
After a little persuasion, my manager agreed to provide my stored procedure for approval, and if approved, it will be created.
If I manage to create a stored procedure, what will be the code to get the desired result?
Thanks in advance.