GRChandrashekar
asked on
Formatted output
Dear Expert friends,
I am stuck with very important financial report. I have below query with attached actual output and required output
Request alternate way of getting the output as per desired format
/* Formatted on 03/05/2011 4:20:44 PM (QP5 v5.136.908.31019) */
SELECT TAX.ABBREVIATION, SUM (TAXDETAIL.TAXAMOUNT) AS TAX, LEDGER.DESCRIPTION
FROM POINTOFSALEBILL
INNER JOIN POINTOFSALEBILLDETAIL
ON POINTOFSALEBILL.POINTOFSAL EBILL_ID =
POINTOFSALEBILLDETAIL.POIN TOFSALEBIL L_ID
INNER JOIN TAXDETAIL
ON POINTOFSALEBILL.POINTOFSAL EBILL_ID =
TAXDETAIL.POINTOFSALEBILL_ ID
AND POINTOFSALEBILLDETAIL.POIN TOFSALEBIL LDETAIL_ID =
TAXDETAIL.POINTOFSALEBILLD ETAIL_ID
INNER JOIN TAX
ON TAX.TAX_ID = TAXDETAIL.TAX_ID
INNER JOIN LEDGER
ON LEDGER.LEDGER_ID = POINTOFSALEBILLDETAIL.LEDG ER_ID
GROUP BY POINTOFSALEBILL.BILLAMOUNT ,
TAX.ABBREVIATION,
TAXDETAIL.TAXAMOUNT,
LEDGER.DESCRIPTION
Pivot.xls
I am stuck with very important financial report. I have below query with attached actual output and required output
Request alternate way of getting the output as per desired format
/* Formatted on 03/05/2011 4:20:44 PM (QP5 v5.136.908.31019) */
SELECT TAX.ABBREVIATION, SUM (TAXDETAIL.TAXAMOUNT) AS TAX, LEDGER.DESCRIPTION
FROM POINTOFSALEBILL
INNER JOIN POINTOFSALEBILLDETAIL
ON POINTOFSALEBILL.POINTOFSAL
POINTOFSALEBILLDETAIL.POIN
INNER JOIN TAXDETAIL
ON POINTOFSALEBILL.POINTOFSAL
TAXDETAIL.POINTOFSALEBILL_
AND POINTOFSALEBILLDETAIL.POIN
TAXDETAIL.POINTOFSALEBILLD
INNER JOIN TAX
ON TAX.TAX_ID = TAXDETAIL.TAX_ID
INNER JOIN LEDGER
ON LEDGER.LEDGER_ID = POINTOFSALEBILLDETAIL.LEDG
GROUP BY POINTOFSALEBILL.BILLAMOUNT
TAX.ABBREVIATION,
TAXDETAIL.TAXAMOUNT,
LEDGER.DESCRIPTION
Pivot.xls
ASKER
@nav_kum_v:
Well the whole of problem arises here...I do not know the abbreviations like 'VAT5' since it comes from DB values.
I dont mind hardcoding 100 Columns for the purpose, but not sure how to go about
Well the whole of problem arises here...I do not know the abbreviations like 'VAT5' since it comes from DB values.
I dont mind hardcoding 100 Columns for the purpose, but not sure how to go about
run this query to get all the distinct codes from the database and hard code them accordingly in my query for a quick and easy thing though it is not the perfect way. You need to use dynamic sql for this kind of requirement.
SELECT distinct TAX.ABBREVIATION
FROM POINTOFSALEBILL
INNER JOIN POINTOFSALEBILLDETAIL
ON POINTOFSALEBILL.POINTOFSAL EBILL_ID =
POINTOFSALEBILLDETAIL.POIN TOFSALEBIL L_ID
INNER JOIN TAXDETAIL
ON POINTOFSALEBILL.POINTOFSAL EBILL_ID =
TAXDETAIL.POINTOFSALEBILL_ ID
AND POINTOFSALEBILLDETAIL.POIN TOFSALEBIL LDETAIL_ID =
TAXDETAIL.POINTOFSALEBILLD ETAIL_ID
INNER JOIN TAX
ON TAX.TAX_ID = TAXDETAIL.TAX_ID
INNER JOIN LEDGER
ON LEDGER.LEDGER_ID = POINTOFSALEBILLDETAIL.LEDG ER_ID
GROUP BY POINTOFSALEBILL.BILLAMOUNT ,
TAX.ABBREVIATION,
TAXDETAIL.TAXAMOUNT,
LEDGER.DESCRIPTION
SELECT distinct TAX.ABBREVIATION
FROM POINTOFSALEBILL
INNER JOIN POINTOFSALEBILLDETAIL
ON POINTOFSALEBILL.POINTOFSAL
POINTOFSALEBILLDETAIL.POIN
INNER JOIN TAXDETAIL
ON POINTOFSALEBILL.POINTOFSAL
TAXDETAIL.POINTOFSALEBILL_
AND POINTOFSALEBILLDETAIL.POIN
TAXDETAIL.POINTOFSALEBILLD
INNER JOIN TAX
ON TAX.TAX_ID = TAXDETAIL.TAX_ID
INNER JOIN LEDGER
ON LEDGER.LEDGER_ID = POINTOFSALEBILLDETAIL.LEDG
GROUP BY POINTOFSALEBILL.BILLAMOUNT
TAX.ABBREVIATION,
TAXDETAIL.TAXAMOUNT,
LEDGER.DESCRIPTION
ASKER
Well getting SELECT distinct TAX.ABBREVIATION is not a issue. Problem is since it pertains to financial data, tax.abbreviation keeps on adding and i have to alter the query every time tax.abbreviation is added to tax table !
ASKER
You need to use dynamic sql for this kind of requirement.
How can i do this
How can i do this
ASKER
OR, is there a way to write a function and pass to SP with loop or stuff like though am not sure of actual way to be done
yes, i can understand.
The only ideal solution is to first execute the distinct tax abbreviation query first to get all the codes in the code and then dynamically prepare the final query (on same lines using sum(decode(..)) ) using dynamic sql to use all the different codes and then execute it.
If you are on 11g, then you can try using pivot/unpivot
The only ideal solution is to first execute the distinct tax abbreviation query first to get all the codes in the code and then dynamically prepare the final query (on same lines using sum(decode(..)) ) using dynamic sql to use all the different codes and then execute it.
If you are on 11g, then you can try using pivot/unpivot
if you want to do it with pl/sql loop / function / procedure etc, then you can do it as you are saying.
1) Have a loop to traverse all the different codes
2) for each code execute the sql and take the results ( but you will not be able to get it in the same format as you need )
3) So you may have to use utl_file package etc to write to file in the format you need.
Thanks
1) Have a loop to traverse all the different codes
2) for each code execute the sql and take the results ( but you will not be able to get it in the same format as you need )
3) So you may have to use utl_file package etc to write to file in the format you need.
Thanks
ASKER
I am on 9i
pl/sql loop / function / procedure etc is ok
ELSE
for each code execute the sql and take the results ( but you will not be able to get it in the same format as you need )
3) So you may have to use utl_file package etc to write to file in the format you need.
This also fine. But not able to get it right. can you please help me
pl/sql loop / function / procedure etc is ok
ELSE
for each code execute the sql and take the results ( but you will not be able to get it in the same format as you need )
3) So you may have to use utl_file package etc to write to file in the format you need.
This also fine. But not able to get it right. can you please help me
ASKER
Preferrably pl/sql loop / function / procedure etc is ok
it is not an easy thing for me to come up with that kind of code because of time issues. But yes, if you can start to come up with that kind of thing, i can help to oversee any issues if you have as and when required.
ASKER
I shall do... atleast a hint to start ..?
ASKER
I meant alteast the steps that a Proc/function/sql should do
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select ledger.description,
sum(decode(tax.abbreviatio
sum(decode(tax.abbreviatio
sum(decode(tax.abbreviatio
from (
SELECT TAX.ABBREVIATION, SUM (TAXDETAIL.TAXAMOUNT) AS TAX, LEDGER.DESCRIPTION
FROM POINTOFSALEBILL
INNER JOIN POINTOFSALEBILLDETAIL
ON POINTOFSALEBILL.POINTOFSAL
POINTOFSALEBILLDETAIL.POIN
INNER JOIN TAXDETAIL
ON POINTOFSALEBILL.POINTOFSAL
TAXDETAIL.POINTOFSALEBILL_
AND POINTOFSALEBILLDETAIL.POIN
TAXDETAIL.POINTOFSALEBILLD
INNER JOIN TAX
ON TAX.TAX_ID = TAXDETAIL.TAX_ID
INNER JOIN LEDGER
ON LEDGER.LEDGER_ID = POINTOFSALEBILLDETAIL.LEDG
GROUP BY POINTOFSALEBILL.BILLAMOUNT
TAX.ABBREVIATION,
TAXDETAIL.TAXAMOUNT,
LEDGER.DESCRIPTION
)
group by ledger.description