Help in what concerns the generation of XML using SQL/XML

I wrote the following SQL to generate a XML report. Tb_ken_lg_mstr of the table is the primary table of the workforce. Tb_ken_loans_base and tb_ken_cards_base tables are tables containing the cards and loans to sales by sales in tb_ken_lg_mstr people.

The problems that I face, is that:

1. even when there are sales people have not made a sale, that is, they have no documents tb_ken_cards_base and tb_ken_loans_base, yet their records are appearing the XML. I want to avoid this and only dirty people who have the list sales. A workaround is to use NOT EXISTS in the outer query more with tb_ken_lg_mstr. Is there a better way?

2 can anyone confirm that the approach used to combine the tables tb_ken_cards_base and tb_ken_loans_base is correct or not. Basically, I want to publish a report of sales person (tb_ken_lg_mstr) and all its sales (tb_ken_cards_base and tb_ken_loans_base).

3. the XML output is coming in a single line rather than in a hierarchical format.

I use 10.2 g
WITH TB_KEN_LG_MSTR AS(
SELECT 97 SALES_CODE , 'Christine' NAMES, 'LOANS' SPECIALIZATION, '03-SEP-07' DATE_REPORTED, TO_DATE('30-JUN-2010') SYSTEM_DATE FROM DUAL UNION ALL 
SELECT 112, 'Winn ', 'LOANS', '03-SEP-07', TO_DATE('30-JUN-2010') FROM DUAL  UNION ALL 
SELECT 093, 'Lydh ', 'LOANS', '03-SEP-07', TO_DATE('30-JUN-2010') FROM DUAL  UNION ALL 
SELECT 107, 'Nai ', 'LOANS', '03-SEP-07', TO_DATE('30-JUN-2010') FROM DUAL  UNION ALL 
SELECT 065, 'Jat', 'LOANS', '03-SEP-07', TO_DATE('30-JUN-2010')  FROM DUAL UNION ALL 
SELECT 980, 'Mreen ', 'LOANS', '27-AUG-07', TO_DATE('30-JUN-2010') FROM DUAL ),
TB_KEN_CARDS_BASE AS(
SELECT 97 SALES_CODE,  46766 REFERENCE_NUMBER, 'BC' PRODUCT_TYPE, 'DECLINE' CURRENT_STATUS, 'I' APPLICANT_IS, TO_DATE('30-JUN-2010') SYSTEM_DATE FROM DUAL UNION ALL 
SELECT 97, 46374, 'BC', 'DECLINE', 'I', TO_DATE('30-JUN-2010') FROM DUAL UNION ALL 
SELECT 112, 46106, 'BC', 'DECLINE', 'I', TO_DATE('30-JUN-2010') FROM DUAL UNION ALL 
SELECT 112, 44263, 'BC', 'APPROVAL', 'I' , TO_DATE('30-JUN-2010')FROM DUAL UNION ALL 
SELECT 112, 46269, 'BC', 'APPROVAL', 'I', TO_DATE('30-JUN-2010') FROM DUAL UNION ALL 
SELECT 093, 4618, 'BC', 'DECLINE', 'I', TO_DATE('30-JUN-2010') FROM DUAL) ,
TB_KEN_LOANS_BASE AS(
SELECT 093 SALES_CODE, 16 BRANCH_NUMBER, '18512' ACCOUNT_NUMBER, '8' ACCOUNT_TYPE, 'DECLINED' FINAL_DECISION, '2000000' LOAN_AMOUNT, '-1' TOP_UP_AMOUNT, TO_DATE('30-JUN-2010') SYSTEM_DATE FROM DUAL UNION ALL 
SELECT 093, 3, '14719', '3', 'DECLINED', '100000', '172.65' , TO_DATE('30-JUN-2010') FROM DUAL UNION ALL 
SELECT 107, 9, '11444', '7', 'AUTHORISED', '200000', '-1', TO_DATE('30-JUN-2010') FROM DUAL UNION ALL 
SELECT 107, 75, '1570', '8', 'AUTHORISED', '2000000', '1626670.3', TO_DATE('30-JUN-2010') FROM DUAL UNION ALL 
SELECT 066, 40, '10229', '7', 'AUTHORISED', '288000', '228478.35', TO_DATE('30-JUN-2010') FROM DUAL)
select xmlelement("SALES_DATA",
       xmlagg(
              xmlelement("SALES_CODE",
                         xmlattributes(c.sales_code),
                         xmlforest(c.names,
                                   c.specialization,
                                   c.date_reported),
                         (select xmlagg(
                                       xmlelement("CARD_DETAILS",
                                                  xmlattributes(a.reference_number),
                                                  xmlforest(a.product_type,
                                                            a.current_status,
                                                            a.applicant_is)
                                                 )
                                       )
                           from tb_ken_cards_base a
                           where system_date=TO_DATE('30-JUN-2010')
                           and a.sales_code = c.sales_code),
                           (select xmlagg(
                                       xmlelement("LOAN_DETAILS",
                                                  xmlattributes(b.branch_number, b.account_number),
                                                  xmlforest(b.account_type,
                                                            b.final_decision,
                                                            b.loan_amount,
                                                            b.top_up_amount)
                                                 )
                                       )
                           from tb_ken_loans_base b
                           where system_date=TO_DATE('30-JUN-2010')
                           and b.sales_code = c.sales_code)
                           )
             )
                )
from tb_ken_lg_mstr c
where system_date=TO_DATE('30-JUN-2010');

Using NOT EXIST would be to do the scan of table twice. What I don't understand is why Oracle is picking up Sales_Codes missing in tb_ken_loans_base or tb_ken_cards_base, even if I do an inner join?

You do not have an inner join, you only access TB_KEN_LG_MSTR in the main query.
What you do in the SELECT part are scalar subqueries just on each of your tables of the child, who is unable to filter the main table in the first place.

NOT EXISTS is indeed a solution, or consider doing all of the joins in the FROM clause, as I suggested above, like this:

...
FROM TB_KEN_LG_MSTR c
     LEFT OUTER JOIN tb_ken_cards_base a ON a.sales_code = c.sales_code
     LEFT OUTER JOIN tb_ken_loans_base b ON b.sales_code = c.sales_code
WHERE a.sales_code IS NOT NULL
OR b.sales_code IS NOT NULL
...

But then the subgroups becomes more difficult with this approach.

Regarding the third question, how I format in Oracle? If not possible in Oracle then what other ways are?

10.2, you can use this trick to format the output:

SELECT xmlelement( ... ).extract('/*').getClobVal() FROM ...

Tags: Database

Similar Questions

Maybe you are looking for

  • FN key/flashcards/toucpad works not-Win7/Vista

    Hello I installed Win7 and Vista to try to make my touchpad works. At this point, I have installed and reinstalled all the touchpad / drivers VAP on the two operating systems. I don't seem to get any results. Ago 0.5 seconds to a time where my touchp

  • Win vista svc pk 2 Update error 80070643, kb2691905

    Tried to install the latest updates for Windows Vista Home Premium with svc pk 2, which included updating of security essential kb2691905 and it failed. MSG for error 80070643. Followed the instructions of Fixit, but cannot return to retry the update

  • Stop the "Recently changed" archive in your history folder

    Do all the files that you use or access appear automatically in the folder "recently changed"? Can you stop the files to be indexed in this file?  I also have files that I've trashed and yet they still appear in the recently changed folder.  What can

  • Previous button doesn't work is not in dialog.ask

    Hello If dialog.ask with 2-3 buttons is displayed on clickin device key return her without closing the dialog box, but dialog.alert is closed How to close the dialog box?

  • Format of physical drive using PERC 6 / i BIOS utility

    I use a Dell R710 server that has raid PERC 6 controller / utility i of the BIOS. I want to use few old hard disks on this server. As they are old, I want to first format. Question: How can I format these physical hard drives using the BIOS RAID Util