Consider the following table and data in a SQL Server 2008 database:
CREATE TABLE sponsorships
(
sponsorshipID INT NOT NULL PRIMARY KEY IDENTITY,
sponsorshipLocationID INT NOT NULL,
sponsorshipArtworkID INT NOT NULL
);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (1, 1);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (1, 2);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (2, 1);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (2, 2);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (3, 3);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (4, 3);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (5, 4);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (6, 1);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (7, 1);
INSERT INTO sponsorships (sponsorshipLocationID, sponsorshipArtworkID)
VALUES (7, 3);
SELECT *
FROM sponsorships s
ORDER BY s.sponsorshipLocationID, s.sponsorshipArtworkID
How can I produce the following output?
CREATE TABLE sponGroups
(
rank INT,
sponsorshipID INT,
sponsorshipLocationID INT,
sponsorshipArtworkID INT
);
INSERT INTO sponGroups VALUES (1, 1, 1, 1);
INSERT INTO sponGroups VALUES (1, 2, 1, 2);
INSERT INTO sponGroups VALUES (1, 3, 2, 1);
INSERT INTO sponGroups VALUES (1, 4, 2, 2);
INSERT INTO sponGroups VALUES (2, 5, 3, 3);
INSERT INTO sponGroups VALUES (2, 6, 4, 3);
INSERT INTO sponGroups VALUES (3, 7, 5, 4);
INSERT INTO sponGroups VALUES (4, 8, 6, 1);
INSERT INTO sponGroups VALUES (5, 9, 7, 1);
INSERT INTO sponGroups VALUES (5, 10, 7, 3);
SELECT *
FROM sponGroups sg
ORDER BY sg.rank, sg.sponsorshipID, sg.sponsorshipLocationID, sg.sponsorshipArtworkID
Fiddle available, here.
Explanation
Artwork is displayed in various locations. Some locations have double-sided artwork installed (e.g. windows), and some single sided (e.g. walls). For example, the artwork at location 1 is double sided--it has sponsorshipArtworkIDs 1 and 2--and there is single-sided artwork at location 5 (sponsorshipArtworkID 4).
For printing and installation purposes, I need a query that yields each piece of artwork, be it one sided or two, and all the locations tied to that piece. (Reference the desired output in the fiddle linked above.) So, for example, I need to tell the printer:
- Print the double-sided artwork (1,2) and install it at locations (1,2);
- Print the single-sided artwork (3) and install it at locations (3,4);
- Print the single-sided artwork (4) and install it at location (5);
- Print the single-sided artwork (1) and install it at location (6);
- Print the double-sided artwork (1,3) and install it at location (7).
Note that art is reused sometimes, so sponsorshipArtworkID 1 is used at both single- and double-sided locations.
I've attempted to use DENSE_RANK(), a recursive CTE, and set-wise divide to solve this problem, but haven't been able to as of yet. Thanks in advance for the help.