Home » SQL & PL/SQL » SQL & PL/SQL » Re: Cross Tab Report
Re: Cross Tab Report [message #19457] Tue, 19 March 2002 14:07
David
Messages: 110
Registered: November 1998
Senior Member
Assuming the following table definition... 

 SQL> desc mytable
   
     Name    Null?    Type
     ------- -------- -----------
     REF_NO           NUMBER(3)
     L_CODE           VARCHAR2(3)
     AMOUNT           NUMBER(8)

And having the following data in the table...

   SQL> select * from mytable;
   
     REF_NO    L_C    AMOUNT
  ---------    --- ---------
        126    IHP     10000
        126    LTE      5000
        127    IHP      6000
        127    IHI      7000

 
The following query, using the DECODE operator, is a fairly simple,
straightforward, extenisble solution to creating a crosstab report...
   
     SELECT ref_no,
            SUM( DECODE(l_code, 'IHP', amount, 0) ) "IHP",
            SUM( DECODE(l_code, 'LTE', amount, 0) ) "LTE",
            SUM( DECODE(l_code, 'IHI', amount, 0) ) "IHI"
       FROM mytable
   GROUP BY ref_no;
 

Which yields the results...

   REF_NO          IHP       LTE       IHI
   --------- --------- --------- ---------
   126           10000      5000         0
   127            6000         0      7000
 
Previous Topic: Unique constraint
Next Topic: Delete Duplicates
Goto Forum:
  


Current Time: Sat Apr 20 08:43:25 CDT 2024