简体   繁体   中英

How do you UNION tables with schema differences?

I am currently working with a data set that relates to the current year. Data that relates to each year is housed in a separate table. When the data is queried, it is done so using a UNION ALL query.

Unfortunately, the data sets provided in the past do not share the same schema as that for the current year, some fields have been added, some retired, and others have been renamed. I have no control over this.

In this case, how am I to do UNION ALL queries across these tables when the schema are different? The differences are not very significant, but they deviate enough to cause problems.

Any suggestions?

Do I merge everything into one large table including all fields spanning across all years and then add new ones as they appear? Or, do I continue to keep these tables separate?

Well, for one, don't try to UNION (actually UNION ALL would probably be more appropriate) with SELECT *.

You can:

  • add columns to the sets that don't have a particular column, with token default / NULL values
  • convert columns that are currently "the same" but use incompatible types
  • simply leave out columns that aren't common enough to bother including

For example:

DECLARE @a TABLE(d DATE, c INT, x FLOAT);

DECLARE @b TABLE(d DATETIME, c VARCHAR(32));

DECLARE @c TABLE(d DATE, x INT, y INT);

SELECT d, c = CONVERT(VARCHAR(32), c), x = CONVERT(INT, x) FROM @a
UNION ALL
SELECT CONVERT(DATE, d), c, x = NULL FROM @b
UNION ALL 
SELECT d, c = 'not supplied', x FROM @c;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM