SELECT red1.partnumber, (blue1.unitssold + red1.unitssold) AS "units sold" FROM red1, blue1 WHERE red1.partnumber = blue1.partnumber UNION SELECT partnumber, unitssold AS "units sold" FROM red1 WHERE partnumber not in (SELECT partnumber FROM blue1) UNION SELECT partnumber, unitssold AS "units sold" FROM blue1 WHERE partnumber not in (SELECT partnumber FROM red1);
First create a table where the descriptions appear to matchpart
CREATE TABLE matchpart ( blue2partnumber INTEGER, red2partnumber INTEGER, partdescription VARCHAR(20), PRIMARY KEY (blue2partnumber, red2partnumber)); INSERT INTO matchpart VALUES (1,1009,'widget - 5cm'); INSERT INTO matchpart VALUES (2,1010,'widget - 10cm'); INSERT INTO matchpart VALUES (6,1006,'widget - blue'); INSERT INTO matchpart VALUES (8,1008,'widget - yellow'); INSERT INTO matchpart VALUES (16,1004,'widget - plain'); SELECT red2.partnumber, matchpart.partdescription, (blue2.unitssold + red2.unitssold) AS "units sold" FROM red2, matchpart, blue2 WHERE red2.partnumber = matchpart.red2partnumber AND blue2.partnumber = matchpart.blue2partnumber UNION SELECT partnumber, partdescription, unitssold AS "units sold" FROM red2 WHERE partnumber not in (SELECT red2partnumber FROM matchpart) UNION SELECT partnumber, partdescription, unitssold AS "units sold" FROM blue2 WHERE partnumber not in (SELECT blue2partnumber FROM matchpart);
This page is part of the promotional and support
material for Data Management (open edition) by Richard T. Watson For questions and comments please contact the author |