位置請看下面的表格和數據的SQL Server 2008數據庫:SQL Server 2008中 - 查找藝術品
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
我怎麼能生產出下面的輸出?
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
小提琴可用,here。
說明
圖形顯示在不同的位置。某些位置安裝了雙面圖稿(例如窗戶),以及一些單面(如牆壁)。例如,位置1的藝術品是雙面的 - 它具有贊助ArtworkIDs 1和2 - 並且位置5處有單面藝術品(贊助商ArtworkID 4)。
對於打印和安裝的目的,我需要一個查詢來產生每件作品,無論是單面還是兩面,以及所有與該作品有關的位置。 (參考上面提到的小提琴中的所需輸出)。例如,我需要告訴打印機:
- 打印雙面圖稿(1,2)並將其安裝在位置(1,2 );
- 打印單面圖稿(3)並將其安裝在位置(3,4);
- 打印單面圖稿(4)並將其安裝在位置(5)處;
- 打印單面圖稿(1)並將其安裝在位置(6);
- 打印雙面圖稿(1,3)並將其安裝在位置(7)。
請注意,藝術有時會被重複使用,所以贊助ArtArtIDID1在單面和雙面位置都會使用。
我試圖使用DENSE_RANK(),一個遞歸CTE,並設置明智的鴻溝來解決這個問題,但還沒有能夠到目前爲止。先謝謝您的幫助。
我已閱讀說明,但仍不知道「rank」列應代表什麼 – Lamak
@SqlZim安裝了雙面圖稿的位置分配了兩件藝術品。例如。位置7分配了兩件藝術品,因此它是雙面位置。 – avejidah
@Lamak「等級」欄是爲了說明我想要的分組;我解釋底部的項目符號列表直接對應於「等級」列。基本上我需要按照5個不同的請求順序將每個項目符號/等級發送給打印機/安裝程序。 – avejidah