Solved

how to sum the amount based on conditions ?

Posted on 2007-11-19
1
1,258 Views
Last Modified: 2013-12-19
I have a view called vw_invoice which has attributes amount, fiscal_key, splr_name, splr_id, adv_flag.

I need to sum all the amounts for current fiscal year, sum all the amounts for all the fiscal periods thus far -  by supplier,sum all the amounts
thus far for supplier where adv_flag is true, sum all the amounts for the current fiscal period where adv_flag is true.

eg: the output would be


splr_id , splr_name, address, amt_current_fiscal, sum_total, amt_current_fiscal_adv_flag, sum_total_adv_flag


1   tesname, houston, 100000.00, 20000000.00, 500000.00, 5430000.00


select splr_id, splr_name, sum(amount) as sum_total from vw_invoice
group by splr_id, splr_name --- this would get me current total amount how would I get the rest ?

the fiscal_key would be a parameter.
0
Comment
Question by:tech_question
1 Comment
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 20312667
SELECT   splr_id, splr_name, address,
         SUM(CASE
                 WHEN fiscal_key = :current_period
                     THEN amount
                 ELSE NULL
             END) amt_current_fiscal, SUM(amount) sum_total,
         SUM(CASE
                 WHEN fiscal_key = :current_period AND adv_flag = 'TRUE'
                     THEN amount
                 ELSE NULL
             END
            ) amt_current_fiscal_adv_flag,
         SUM(CASE
                 WHEN adv_flag = 'TRUE'
                     THEN amount
                 ELSE NULL
             END) sum_total_adv_flag
    FROM vw_invoice
GROUP BY splr_id, splr_name, address
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

839 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