0

I have a table test3 which has the data below. I want to apply aggregate function based on status.

request mkt_val fill_val qty fill_qty rate c_party status
------- ------- -------- --- -------- ---- ------- ------
    IBM     200      100  20       10    1   test1 Accept
    IBM     300      200  30       20    2   test1 Accept
    IBM     400      300  40       30    3   test1 Accept
    IBM     500      400  50       40    4   test2 Reject
    IBM     600      500  60       50    5   test2 Reject

I need output with the columns:

  • request
  • a_sum_mkt_val
  • a_avg_fill_qty
  • a_min_rate
  • r_sum_mkt_val
  • r_avg_fill_qty
  • r_min_rate
  • c_cnt (distinct c_party for request)
  • total_req (total number of records for request)

Prefix a_ for accept, r_ for reject. For the sample data, the output should be:

IBM  900  20  1  1100  45  4  2  5

I am not able to categorise the value based on the status.

5
  • Which RDBMS are you using? Commented Nov 28, 2013 at 9:10
  • its a oracle database.oracle 10g database table test3 Commented Nov 28, 2013 at 9:21
  • It would be very helpful if you could post some DDL for creating your table and inserting data into it. Commented Nov 28, 2013 at 9:30
  • What do you mean by a_sum_ and r_sum_? Commented Nov 28, 2013 at 9:35
  • INSERT INTO test3 VALUES('IBM',200,100,20,10,1,'test2','Reject'); INSERT INTO test3 VALUES('IBM',300,200,30,20,2,'test2','Reject'); INSERT INTO test3 VALUES('IBM',400,300,40,30,3,'test2','Reject'); INSERT INTO test3 VALUES('IBM',500,400,50,40,4,'test2','Reject'); insert into test3 values('IBM',600,500,60,50,5,'test2','Reject'); a_sum means sum FOR accept record r_sum means sum for reject record requets a_sum_mkt_val r_sum_mkt_val IBM 900 1100 Commented Nov 28, 2013 at 9:55

1 Answer 1

3

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE tbl ( request, mkt_val, fill_val, qty, fill_qty, rate,  c_party,  status ) AS
          SELECT 'IBM',     200,      100,  20,       10,    1,   'test1', 'Accept' FROM DUAL
UNION ALL SELECT 'IBM',     300,      200,  30,       20,    2,   'test1', 'Accept' FROM DUAL
UNION ALL SELECT 'IBM',     400,      300,  40,       30,    3,   'test1', 'Accept' FROM DUAL
UNION ALL SELECT 'IBM',     500,      400,  50,       40,    4,   'test2', 'Reject' FROM DUAL
UNION ALL SELECT 'IBM',     600,      500,  60,       50,    5,   'test2', 'Reject' FROM DUAL;

Query 1:

SELECT request,
       SUM( DECODE( status, 'Accept', mkt_val,  NULL ) ) AS a_sum_mkt_val,
       AVG( DECODE( status, 'Accept', fill_qty, NULL ) ) AS a_avg_fill_qty,
       MIN( DECODE( status, 'Accept', rate,     NULL ) ) AS a_min_rate,
       SUM( DECODE( status, 'Reject', mkt_val,  NULL ) ) AS r_sum_mkt_val,
       AVG( DECODE( status, 'Reject', fill_qty, NULL ) ) AS r_avg_fill_qty,
       MIN( DECODE( status, 'Reject', rate,     NULL ) ) AS r_min_rate,
       COUNT( DISTINCT c_party ) AS c_cnt,
       COUNT( 1 ) AS total_req
FROM   tbl
GROUP BY request

Results:

| REQUEST | A_SUM_MKT_VAL | A_AVG_FILL_QTY | A_MIN_RATE | R_SUM_MKT_VAL | R_AVG_FILL_QTY | R_MIN_RATE | C_CNT | TOTAL_REQ |
|---------|---------------|----------------|------------|---------------|----------------|------------|-------|-----------|
|     IBM |           900 |             20 |          1 |          1100 |             45 |          4 |     2 |         5 |
Sign up to request clarification or add additional context in comments.

1 Comment

Good, but if you are happy with it then please accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.