Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help with Window Functions

$
0
0

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:

ConsignmentIDLabel NumberConsignment Reference Sequence Number
1Label11
1Label21
1Label33
1Label43
1Label53
1Label63
2Label11
2Label21
2Label31
3Label11
3Label23
3Label34


Thanks for reading.

Cheers, Clay


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>