SQL Server 2008-查找艺术品的位置
考虑SQL Server 2008数据库中的下表和数据:SQL Server 2008-查找艺术品的位置,sql,sql-server,sql-server-2008,dense-rank,Sql,Sql Server,Sql Server 2008,Dense Rank,考虑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, sponsors
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
小提琴可用
解释
艺术品陈列在不同的地方。一些位置安装了双面艺术品(如窗户),一些位置安装了单面艺术品(如墙壁)。例如,位置1处的艺术品是双面的——它有sponsorshipArtworkID 1和2——而位置5处的艺术品是单面的(sponsorshipArtworkID 4)
出于打印和安装目的,我需要一个查询来生成每件艺术品,无论是单面还是双面,以及与该作品相关的所有位置。(参考上面链接的小提琴中所需的输出。)因此,例如,我需要告诉打印机:
- 打印双面艺术品(1,2)并将其安装在位置(1,2)李>
- 打印单面艺术品(3)并将其安装在位置(3,4)李>
- 打印单面艺术品(4)并将其安装在位置(5)李>
- 打印单面艺术品(1)并将其安装在位置(6)李>
- 打印双面艺术品(1,3)并将其安装在位置(7)处
我曾尝试使用DENSE_RANK()(一种递归CTE)和set-wise divide来解决这个问题,但到目前为止还没有实现。提前感谢您的帮助。这可能看起来有点难看,但想法很简单 首先按
sponsorshipLocationID
分组,并用关联的sponsorshipArtworkID
列表构建一个逗号分隔的字符串
然后根据这个以逗号分隔的艺术品ID字符串计算稠密等级
在下面的查询中,我使用FOR XML
连接字符串。这不是唯一的方法。有很好的快速CLR函数可以实现这一点
我建议一步一步地运行下面的查询,逐个CTE并检查中间结果,以了解其工作原理
样本数据
DECLARE @sponsorships TABLE
(
sponsorshipID INT NOT NULL PRIMARY KEY IDENTITY,
sponsorshipLocationID INT NOT NULL,
sponsorshipArtworkID INT NOT NULL
);
INSERT INTO @sponsorships (sponsorshipLocationID, sponsorshipArtworkID) VALUES
(1, 1),
(1, 2),
(2, 1),
(2, 2),
(3, 3),
(4, 3),
(5, 4),
(6, 1),
(7, 1),
(7, 3);
查询
WITH
CTE_Locations
AS
(
SELECT
sponsorshipLocationID
FROM
@sponsorships AS S
GROUP BY
sponsorshipLocationID
)
,CTE_Artworks
AS
(
SELECT
CTE_Locations.sponsorshipLocationID
,CA_Data.Artwork_Value
FROM
CTE_Locations
CROSS APPLY
(
SELECT CAST(S.sponsorshipArtworkID AS varchar(10)) + ','
FROM
@sponsorships AS S
WHERE
S.sponsorshipLocationID = CTE_Locations.sponsorshipLocationID
ORDER BY
S.sponsorshipArtworkID
FOR XML PATH(''), TYPE
) AS CA_XML(XML_Value)
CROSS APPLY
(
SELECT CA_XML.XML_Value.value('.', 'NVARCHAR(MAX)')
) AS CA_Data(Artwork_Value)
)
,CTE_Rank
AS
(
SELECT
sponsorshipLocationID
,Artwork_Value
,DENSE_RANK() OVER (ORDER BY Artwork_Value) AS r
FROM CTE_Artworks
)
SELECT
CTE_Rank.r
,S.sponsorshipID
,CTE_Rank.sponsorshipLocationID
,S.sponsorshipArtworkID
FROM
CTE_Rank
INNER JOIN @sponsorships AS S
ON S.sponsorshipLocationID = CTE_Rank.sponsorshipLocationID
ORDER BY
S.sponsorshipID
;
结果
+---+---------------+-----------------------+----------------------+
| r | sponsorshipID | sponsorshipLocationID | sponsorshipArtworkID |
+---+---------------+-----------------------+----------------------+
| 2 | 1 | 1 | 1 |
| 2 | 2 | 1 | 2 |
| 2 | 3 | 2 | 1 |
| 2 | 4 | 2 | 2 |
| 4 | 5 | 3 | 3 |
| 4 | 6 | 4 | 3 |
| 5 | 7 | 5 | 4 |
| 1 | 8 | 6 | 1 |
| 3 | 9 | 7 | 1 |
| 3 | 10 | 7 | 3 |
+---+---------------+-----------------------+----------------------+
秩的实际值与预期结果中的值不完全相同,但它们对行进行了正确分组。我已经阅读了说明,但仍然不知道
rank
列应该是什么represent@SqlZim安装双面艺术品的位置指定了两件艺术品。例如,位置7分配了两件艺术品,因此它是一个双面位置。@Lamak“rank”列用于说明我想要的分组;我解释底部的项目符号列表直接对应于“排名”列。基本上,在本例中,我需要按顺序将每个项目符号/列组作为5个单独的请求发送给打印机/安装程序。我知道它代表您想要的结果。拉马克说:“我只是不知道该怎么解释,但很难用语言来表达这个问题。”。如果你能具体告诉我解释的哪一部分很难理解,那么我将尝试澄清问题的这一部分。谢谢弗拉基米尔,这确实实现了目标。看起来这完全是可以实现的(没有连接),但是这比我提出的任何方法都好。非常感谢您的帮助。@avejidah,我在这里的回答中也有类似的方法和解决方案。我用CHECKSUM\u AGG
给出了一个近似解。