Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to sum the amount based on conditions ?

Posted on 2007-11-19
1
Medium Priority
?
1,265 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
[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
1 Comment
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious sideā€¦
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

610 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