Click here to Skip to main content
15,889,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query as below:
SQL
select  decode(FIN01_NAME,'Income',FIN02_NAME)income,
        decode(FIN01_NAME,'Expenditure',FIN02_NAME)Expenditure,
        decode(FIN01_NAME,'Fixed Assets',FIN02_NAME)FixedAssets,
        decode(FIN01_NAME,'Fixed Liability',FIN02_NAME)FixedLiability,
        decode(FIN01_NAME,'Current Assests',FIN02_NAME)CurrentAssests,
        decode(FIN01_NAME,'Current Liability',FIN02_NAME)CurrentLiability from (
SELECT   FIN01_name,  FIN02_NAME
  FROM      FIN02_CATEGORY
         JOIN
            FIN01_GROUP
         USING (FIN01_ID) order by FIN01_ID );


It gives an output like,


SQL
INCOME	EXPENDITURE FIXEDASSETS	FIXEDLIABILITY	CURRENTASSESTS	CURRENTLIABILITY

Donation					
Grants					
	Goodwill				
		Printer			
		Computer			
		Furniture			
		PrinterRepairCost			
		Upgrading S/w			
			        BankLoan		
			        LongTermDepts		
				              ShortTermInvesting	
				               Cash	
					                         Tax
					                         VaccationPay

but i want an output,as follows:
SQL
INCOME  EXPENDITURE FIXEDASSETS FIXEDLIABILITY  CURRENTASSESTS  CURRENTLIABILITY

Donation Goodwill   Printer       BankLoan      ShortTermInvesting  Tax
Grants              Computer      LongTermDepts Cash                VaccationPay
                    Furniture
                    PrinterRepairCost
                    Upgrading S/w


Can you give any suggestion..????
Thanks..
ajith.
Posted
Updated 11-Feb-13 1:17am
v2
Comments
Jörgen Andersson 14-Feb-13 12:21pm    
Would you mind updating the question with the Oracle version you're using?

1 solution

SQL
WITH CTE AS (
    SELECT  FIN01_name
           ,FIN02_NAME
           ,ROW_NUMBER( ) OVER (PARTITION BY FIN01_name ORDER BY FIN02_NAME NULLS LAST) rn
    FROM    FIN02_CATEGORY
    JOIN    FIN01_GROUP
    USING   FIN01_ID
    )
SELECT  *
FROM    CTE
PIVOT   (
    Max(FIN02_NAME)
    FOR FIN01_name IN ('Income','Expenditure','Fixed Assets','Fixed Liability','Current Assests','Current Liability')
    )
;
This could work in Oracle 11G R2, won't work in earlier versions and it's untested.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900