Good day,More fun with data from a proprietary app's tables! We're returning data with a view from 'current' and 'history' transaction tables. The way this app is set up, each month all 'closed' transactions are moved to the history table. That part's not so bad; using UNION we can get data from both as the columns are similar.However, open transactions also get copied to history -- but are left in the current table too, with a flag of 'H' in the history column. These records may be updated in the 'current' table during the month before they get moved or copied to 'history', so may exist with a newer 'version' in the 'current' table. Unfortunately, the history flag is not set on the copies of those records in the history table!I'm looking for an efficient way to union tables in the view but exclude from the 'history' recordset those that still exist in 'current'.Example tables and data:SET NOCOUNT ON-- the current transaction tableCREATE TABLE trans_curr ( order_no INT NOT NULL , line_no INT NOT NULL , history VARCHAR(1) NULL , status VARCHAR(10) NOT NULL )GO-- the transaction history tableCREATE TABLE trans_hist ( order_no INT NOT NULL , line_no INT NOT NULL , status VARCHAR(10) NOT NULL )GO-- some sample data for the current tableINSERT INTO trans_curr VALUES (41797,3,'H','P')INSERT INTO trans_curr VALUES (42650,13,'','P')INSERT INTO trans_curr VALUES (41894,1,'H','C')INSERT INTO trans_curr VALUES (42890,4,'','N')INSERT INTO trans_curr VALUES (39895,7,'H','P') -- this is a new item for order_no 39895INSERT INTO trans_curr VALUES (42810,6,'','P')INSERT INTO trans_curr VALUES (42892,8,'','N')INSERT INTO trans_curr VALUES (43172,11,'','P')INSERT INTO trans_curr VALUES (43153,3,'','P')INSERT INTO trans_curr VALUES (39895,3,'H','P')-- some sample data for the history tableINSERT INTO trans_hist VALUES (41797,3,'P')INSERT INTO trans_hist VALUES (21916,1,'C')INSERT INTO trans_hist VALUES (27043,3,'C')INSERT INTO trans_hist VALUES (39895,3,'P')INSERT INTO trans_hist VALUES (37508,1,'C')INSERT INTO trans_hist VALUES (26327,5,'C')INSERT INTO trans_hist VALUES (11394,3,'C')INSERT INTO trans_hist VALUES (6039,2,'C')INSERT INTO trans_hist VALUES (14767,1,'C')INSERT INTO trans_hist VALUES (41894,1,'P')SET NOCOUNT OFF
The status column unfortunately is not very useful here; of more importance is that new line items may be added for an order_no in trans_curr before it is completed, so if we exclude those with a 'history' value of 'H' from the current table we'll miss some records. We have to exclude from the history table those order numbers with a value of 'H' in the current table.For example, you will see that I commented the record for order_no 39895, line_no 7; this is a new line item that was added to that order after the monthly move/copy batch. However, the app sets its 'history' flag to 'H' so that it matches the other items on the order, some of which have been moved to history. So, if we exclude items marked history = 'H', we miss those items.This is unneccessarily complex and difficult, but I didn't write the app... Here's what I've got:SELECT order_no , line_no-- , etc...FROM trans_currUNION ALLSELECT order_no , line_no-- , etc...FROM trans_histWHERE order_no NOT IN ( SELECT order_no FROM trans_curr WHERE history = 'H' )-- clean up after ourselvesDROP TABLE trans_currGODROP TABLE trans_histGO
This works, but really is not aesthetically pleasing. The real history table in particular is pretty big, several million records, and the actual tables have many dozens of columns each. Four our views we're pulling data from several sets of current/history tables at once, for each set having to do this WHERE ?? NOT IN (SUBQUERY). However, I can't think of any other way to write this. Can anyone offer suggestions (other than finding the developers and kicking them a bit)?Thanks,Daniel