- Local time
- Today, 02:30
- Joined
- Feb 28, 2001
- Messages
- 30,495
I'll try to offer a reason why union queries are helpful.
Imaging a company selling stuff with multiple sources for that stuff, multiple wholesale and retail customers, and a shrinkage plan for damaged, lost, or pilfered stuff, and even perhaps an archival checkpoint (where you remove really old records to save space and just put a summary record to maintain continuity.). You would have two choices.
1. Make every one of those things fit into a single record (which might be tricky since the shrinkage data and the wholesale data might not fit well together), OR
2. Make each data class have its own record format - but then to figure our inventory of stuff on hand, you use UNION queries that select common elements like the "stuff" inventory number, date of transaction, and number of items of "stuff" covered by the transaction. The UNION query can even make stuff "gains" (arrival of new stock, stock returns, etc.) have the opposite sign to stuff "losses" (customer purchases, shrinkage, etc.). Then your stock on hand at any date is merely the sum of all UNION records with dates on/before the selected date. Which gives you the ability to do running inventory totals, stock turnover rate, etc.
Case 2 is where UNION queries can shine when done well.
Imaging a company selling stuff with multiple sources for that stuff, multiple wholesale and retail customers, and a shrinkage plan for damaged, lost, or pilfered stuff, and even perhaps an archival checkpoint (where you remove really old records to save space and just put a summary record to maintain continuity.). You would have two choices.
1. Make every one of those things fit into a single record (which might be tricky since the shrinkage data and the wholesale data might not fit well together), OR
2. Make each data class have its own record format - but then to figure our inventory of stuff on hand, you use UNION queries that select common elements like the "stuff" inventory number, date of transaction, and number of items of "stuff" covered by the transaction. The UNION query can even make stuff "gains" (arrival of new stock, stock returns, etc.) have the opposite sign to stuff "losses" (customer purchases, shrinkage, etc.). Then your stock on hand at any date is merely the sum of all UNION records with dates on/before the selected date. Which gives you the ability to do running inventory totals, stock turnover rate, etc.
Case 2 is where UNION queries can shine when done well.