Oracle group by function

I have a table as shown below and in the PRICE_VOLUME_COST_INDICATOR column, there are three distinct values namely

  1. COST
  2. PRICE
  3. VOLUME

Table structure

And I want to perform

SELECT PRICE_VOLUME_COST_INDICATOR,
       SUM(AMOUNT) 
FROM MY_TABLE 
GROUP BY PRICE_VOLUME_COST_INDICATOR

but I don't want "PRICE" record to be grouped. For "PRICE" records alone I want any amount value.

I am expecting the output below:

enter image description here

Upvotes: 0

Views: 88

Answers (1)

Mureinik
Mureinik

Reputation: 311163

Since, according to the comment, all the "price" records have the same amount, you could use the aggregate max (or min) to just get one of them, and a case expression to choose between max and sum depending on the price_volume_cost_indicator:

SELECT   price_volume_cost_indicator, 
         CASE price_volume_cost_indicator WHEN 'PRICE' THEN MAX(amount)
                                          ELSE SUM(amount)
         END
FROM     my_table
GROUP BY price_volume_cost_indicator

Upvotes: 2

Related Questions