Thursday, 3 December 2015

XML : How do I create single row publication records with multiple authors in SQL in order to generate XML?

Some context: I have an Oracle DB with publication records. I am trying to generate XML files, containing all publications authored or edited by a specific person. However, the XML needs to contain the co-authors as well (and a bunch of other information, which does not cause any trouble).

Relevant code:

  select  NAME_FIRST_NAME,          NAME_LAST_NAME,          LIST_INDEX,          PUBLICATION_ID    from CLASSIFIED_AUTHOR_ASSOC CAA  join PUBLICATION        PUB  on CAA.PUBLICATION_ID = PUB.ID    where CAA.PUBLICATION_ID IN       (select PUBLICATION_ID       from CLASSIFIED_AUTHOR_ASSOC       where PERSON_ID = '127746')    order by CAA.PUBLICATION_ID, CAA.LIST_INDEX ASC    

Sample output:

Bob Johnson 0 996822

Bob Johnson 0 962544

Bob Johnson 0 455211

Sue Hopkins 1 455211

The list_index value is important because it indicates the order of the authors, which is relevant. The issue is that I have multiple rows for a single publication.

I have been looking into pivoting functions, but they all seem oriented towards numerical values and imply that you know in advance how many rows you will end up with (the records in my data set have anything between 1 and 30 authors). My question is: how do I best approach this problem? Do I aggregate the authors in a single column? In this case, I have no idea how to get them in distinct xml-elements afterwards. Or is there another way to achieve what I want to do?

Any pointers in the right direction would be greatly appreciated!

No comments:

Post a Comment