Skip to content
Advertisement

How do I combine multiple tables? (First has data from this month, second has all other previous data)

I am looking to create a query that shows shipping number, the container ID, the tracking number, the location it was last moved to, what time it was moved, and who moved it.

Here’s the issue. We recently backed up or transaction history onto another table for anything that’s over 30 days old.

So I have the table transaction_history which gives me everything from today to 30 days ago, and I have the table AR_transaction_history, which gives me everything else (starting from 31 days ago.)

I need to be able to create prompts for the user to input either the container ID, tracking number, or shipping ID.

I need help joining the two tables to create 1 table with all the records. I tried union all and it does not work with my prompts. I tried an isnull statement and that didn’t work either. Here is the code.

Advertisement

Answer

Do UNION ALL in a subquery, and leave the rest of your original query untouched. This is the simplest way to proceed, without reviewing the whole logic of your (aggregated) query.

Note: in general, SELECT * and UNION ALL do not get along well. This answer assumes that tables TRANSACTION_HISTORY and AR_TRANSACTION_HISTORY have exactly the same structure (columns and data types).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement