Hi all,
I need some help in getting rid of a very slow cursor operation.
I have three tables:
1. Consignments
2. Labels
3. ConsignmentReferences
Labels table has a foreign key to Consignments. ConsginmentReferences also has a foreign key to Consginments.
I need to 'manufacture' a relationship between ConsgnmentReferences and the Labels table. Probably best shown with some example data:
Note: my solution must work on SQL 2008R2 which does not support SUM OVER ORDER BY
USE tempdb; CREATE TABLE Consignments ( ConsignmentID Int Primary Key ) GO CREATE TABLE Labels ( LabelID BigInt Identity Primary Key, LabelNo varchar (20), SequenceNo Int, ConsignmentID Int Foreign Key References Consignments (ConsignmentID) ) GO CREATE TABLE ConsignmentReferences ( ConsignmentReferenceID Int Identity, ConsignmentID Int Foreign Key References Consignments (ConsignmentID), NumberOfItems Int, SequenceNo Int) GO /* INSERT THREE CONSIGNMENTS */ INSERT INTO Consignments VALUES (1) ; INSERT INTO Consignments VALUES (2) ; INSERT INTO Consignments VALUES (3) ; /* INSERT 6 LABELS AGAINST CONSIGNMENT # 1 */ INSERT INTO Labels VALUES ('Label1', 1, 1); INSERT INTO Labels VALUES ('Label2', 2, 1); INSERT INTO Labels VALUES ('Label3', 3, 1); INSERT INTO Labels VALUES ('Label4', 4, 1); INSERT INTO Labels VALUES ('Label5', 5, 1); INSERT INTO Labels VALUES ('Label6', 6, 1); /* INSERT THREE REFERENCE LINES Note: Reference line 2 has no items, therefore, freight labels will not be 'allocated' to this reference line */ INSERT INTO ConsignmentReferences VALUES (1, 2, 1); INSERT INTO ConsignmentReferences VALUES (1, 0, 2); INSERT INTO ConsignmentReferences VALUES (1, 4, 3); /* INSERT 6 LABELS AGAINST CONSIGNMENT # 1 */ INSERT INTO Labels VALUES ('Label1', 1, 2); INSERT INTO Labels VALUES ('Label2', 2, 2); INSERT INTO Labels VALUES ('Label3', 3, 2); /* INSERT ONE REFERENCE LINE FOR CONSIGNMENT # 2 */ INSERT INTO ConsignmentReferences VALUES (2, 3, 1); /* THREE LABELS FOR CONSGINMENT # 3 */ INSERT INTO Labels VALUES ('Label1', 1, 3); INSERT INTO Labels VALUES ('Label2', 2, 3); INSERT INTO Labels VALUES ('Label3', 3, 3); /* FOUR CONSGINMENT REFERENCES ON CONSIGNMENT # 3 */ INSERT INTO ConsignmentReferences VALUES (3, 1, 1); INSERT INTO ConsignmentReferences VALUES (3, 0, 2); INSERT INTO ConsignmentReferences VALUES (3,1, 3); INSERT INTO ConsignmentReferences VALUES (3,1, 4); SELECT L.* FROM Consignments C JOIN Labels L ON C.ConsignmentID = L.ConsignmentID; SELECT CR.* FROM Consignments C JOIN ConsignmentReferences CR ON CR.ConsignmentID = C.ConsignmentID; IF OBJECT_ID('ConsignmentReferences', 'U') IS NOT NULL DROP TABLE ConsignmentReferences; GO IF OBJECT_ID('Labels', 'U') IS NOT NULL DROP TABLE Labels; GO IF OBJECT_ID('Consignments', 'U') IS NOT NULL DROP TABLE Consignments; GO
For each consignment reference I need to x labels, where x is the ConsignmentReference's NumberOfItems.
For example, if Consignment Reference line 1 has NumberOfItems = 3, then the first three Labels are assigned to ConsignmentReference line 1.
Here is the desired output:
ConsignmentID | Label Number | Consignment Reference Sequence Number |
1 | Label1 | 1 |
1 | Label2 | 1 |
1 | Label3 | 3 |
1 | Label4 | 3 |
1 | Label5 | 3 |
1 | Label6 | 3 |
2 | Label1 | 1 |
2 | Label2 | 1 |
2 | Label3 | 1 |
3 | Label1 | 1 |
3 | Label2 | 3 |
3 | Label3 | 4 |
Thanks for reading.
Cheers, Clay