Conventions -
Table A: Release (include if R)
Table B: Comp (include if 0)
Goal: Locate Open Jobs (R) with open operations (0) but getting the operation number that is first after operation Comp = 1 for the given Job and Suffix.
I am stuck on limiting the records to the Goal. I suspect a subquery is involved (could be wrong) but can't get the correct syntax to deliver the correct recordset.
Output desired:
Job Suffix Item Qty OperNum WC Descr
1000 0 AAA 10 30 10 tiny machine
1001 1 BBB 12 60 55 large machine
1002 0 CCC 15 30 20 tinyerest machine
1003 0 AAA 17 40 55 large machine
1005 0 EEE 15 30 20 tinyerest machine
1005 1 EEE 16 60 60 huge machine
Test Data:
CREATE TABLE SyteLine.dbo.A (
Job INT,
Suffix INT,
Item nvarchar(30),
Qty INT,
Release nvarchar(1)
);
GO
INSERT INTO SyteLine.dbo.A (job, Suffix, Item, Qty, Release)
VALUES (1000, 0, 'AAA', 10, 'R'),
(1001, 0, 'BBB', 22, 'R'),
(1001, 1, 'BBB', 12, 'R'),
(1002, 0, 'CCC', 15, 'R'),
(1003, 0, 'AAA', 17, 'R'),
(1004, 0, 'DDD', 10, 'C'),
(1005, 0, 'EEE', 15, 'C'),
(1005, 1, 'EEE', 16, 'R');
GO
CREATE TABLE SyteLine.dbo.B (
Job INT,
Suffix INT,
OperNum INT,
WC INT,
Comp INT
);
GO
INSERT INTO SyteLine.dbo.B (Job, Suffix, OperNum, WC, Comp)
VALUES (1000, 0, 30, 10, 0),
(1000, 0, 20, 20, 1),
(1001, 0, 40, 30, 1),
(1001, 0, 50, 40, 1),
(1001, 0, 60, 55, 0),
(1001, 1, 40, 55, 0),
(1002, 0, 20, 10, 1),
(1002, 0, 30, 20, 0),
(1002, 0, 40, 30, 0),
(1003, 0, 30, 40, 1),
(1003, 0, 40, 55, 0),
(1003, 0, 50, 60, 0),
(1004, 0, 40, 30, 1),
(1005, 0, 30, 20, 0),
(1005, 1, 50, 55, 1),
(1005, 1, 60, 60, 0);
GO
CREATE TABLE SyteLine.dbo.C (
WC INT,
Descr nvarchar(30)
);
GO
INSERT INTO SyteLine.dbo.C (WC, Descr)
VALUES (10, 'tiny machine'),
(20, 'tinyerest machine'),
(30, 'small machine'),
(40, 'medium machine'),
(55, 'large machine'),
(60, 'huge machine');
GO
Regards,
Lance