Wednesday, December 14, 2011

How do you split the data from an SQL Union All clause into another column?

Exactly what are you trying to accomplish here? What is the relation between the two tables and how is it defined? A union basically just combines two or more subtables into one result, which is just what you got - your set of rows from your first table, followed by your set of rows from the second (padded by nulls, since the number of columns of a union must match in number and type.) If the two tables are supposed to be related just by physical order (row 1 of table A relates to row 1 of table B, etc.), be aware that this will not necessarily work in every DBMS - physical order of data rows withing tables does not necessarily relate to the order of rows in a result set, especially if database objects like indexes exist (and almost certainly not if you do any ordering in your query explicitly or implicitly!)

0 comments:

Post a Comment