I have 14 tables that describe a hierarchical XML data structure. One field is a key for the row and another field is a foreign key to its parent. The remaining 60 fields are the same in each table.
I would like to get one large serial list of rows instead of horizontally adding the columns.
For example the following is set up to give me 6 columns. How can I serialize it into 3 columns and more rows?
SELECT ICN2.ICNID, ICN2.ICNTITLE, ICN2.PANE, ICN3.ICNID AS ICNID2,
ICN3.ICNTITLE AS ICNTITLE2, ICN3.PANE AS PANE2
FROM ICN2
LEFT OUTER JOIN ICN3 ON ICN2.ICN2_PKEY = ICN3.ICN2_FKEY
WHERE ICN2.ICNID = N'65587'
Use an SQL
UNIONto create a single (aliased) result set, then apply the condition:This is the syntax for mysql and postgres, but the exact syntax may vary depending on which database you are using.
If you need to know which table the rows came from, add a constant to each row:
BTW
UNION ALLpreserves ordering and duplicate rows. PlainUNIONsorts and removes duplicate rows. Chose which ever suits you best