您可以运行所有这些SQL并查看结果here。
我有一张俱乐部桌(一个由一群人或一群人组成的俱乐部,例如“游泳俱乐部”或“编织俱乐部”)。
DECLARE @club TABLE (
Id INT
,Name NVARCHAR(255)
);
INSERT INTO @club VALUES
(1, 'Swim Club')
,(2, 'Knitting Club')
,(3, 'Bridge Club');
我有一张会员桌。
DECLARE @member TABLE (
Id INT
,Name NVARCHAR(255)
);
INSERT INTO @member VALUES
(1, 'John Jones')
,(2, 'Sally Smith')
,(3, 'Rod Roosevelt')
,(4, 'Bobby Burns')
,(5, 'Megan Moore');
会员可以属于许多俱乐部,因此有一个会员表将俱乐部与会员联系起来(并描述了会员费价格)。
DECLARE @membership TABLE (
Id INT
,Member INT --FK to @member
,Club INT --FK to @club
,Dues INT --the cost of membership
);
INSERT INTO @membership VALUES
(1,1,1,10)
,(2,1,2,5)
,(3,2,1,10)
,(4,2,3,20)
,(5,3,1,10)
,(6,3,2,5)
,(7,4,2,5)
,(8,4,3,20)
,(9,5,1,10)
,(10,5,3,20);
大多数会员只需支付相关的会费即可。但是,某些成员是由其他成员赞助的。因此,这些受保会员将由另一名会员(保荐人)支付会费。因此,我们有一个赞助表。 “赞助商”表将赞助商(支付会费)连接到特定俱乐部的Sponsee(由赞助商支付其会费)。因为赞助是特定于俱乐部的,所以赞助记录将连接两个会员记录,而不是两个会员记录。
DECLARE @sponsorship TABLE (
Id INT
,Sponsee_Membership INT --FK to Sponsee's @membership record
,Sponsor_Membership INT --FK to Sponsor's @membership record
);
INSERT INTO @sponsorship VALUES
(1,5,1)
,(2,8,4)
,(3,9,3)
,(4,10,4);
要全面了解我们的俱乐部/会员资格/赞助商,我们有:
SELECT
mship.Id AS 'Mship'
,mem.Name AS 'Member'
,c.Name AS 'Club'
,mship.Dues
,spons_mem.Name AS 'Sponsor'
FROM
@membership AS mship
JOIN @member AS mem
ON mship.Member = mem.Id
JOIN @club AS c
ON mship.Club = c.Id
LEFT JOIN @sponsorship AS spons
ON spons.Sponsee_Membership = mship.Id
LEFT JOIN @membership AS spons_mship
ON spons_mship.Id = spons.Sponsor_Membership
LEFT JOIN @member AS spons_mem
ON spons_mem.Id = spons_mship.Member;
为我们提供了这些结果:
Mship Member Club Dues Sponsor
1 John Jones Swim Club 10 NULL
2 John Jones Knitting Club 5 NULL
3 Sally Smith Swim Club 10 NULL
4 Sally Smith Bridge Club 20 NULL
5 Rod Roosevelt Swim Club 10 John Jones
6 Rod Roosevelt Knitting Club 5 NULL
7 Bobby Burns Knitting Club 5 NULL
8 Bobby Burns Bridge Club 20 Sally Smith
9 Megan Moore Swim Club 10 Sally Smith
10 Megan Moore Bridge Club 20 Sally Smith
赞助应涵盖所有共享的会员资格。
Mship=7
和Mship=8
行中看到它。 很抱歉,设置很长。 这是我的实际问题:
Mship=5
和Mship=6
行。 我可以使用游标/ WHILE循环来完成此操作,但是我知道这样的解决方案通常不会采用适当的基于集合的方法。对此的适当查询应该是什么样的? 非常感谢。
答案 0 :(得分:1)
这是一个可能会响应您的要求的SQL查询。
逻辑是根据映射member.id
而不是memberships.id
使用子查询在赞助者和受赞助者之间生成映射;为此,我们使用聚合。然后,外部查询会搜索赞助商和赞助方都参加但在赞助表中未声明任何关联的俱乐部
查询为每个违规成员资格返回一条记录,其中包含赞助人名称和赞助人名称。
SELECT mship1.Id, m1.Name Member, m2.Name Sponsor, c.Name Club, mship1.Dues
FROM
@membership mship1
INNER JOIN @club c ON c.Id = mship1.Club
INNER JOIN (
SELECT ms1.Member Sponsee_Member , MAX(ms2.Member) Sponsor_Member
FROM @sponsorship ss
INNER JOIN @membership ms1 ON ms1.Id = Sponsee_Membership
INNER JOIN @membership ms2 ON ms2.Id = Sponsor_Membership
GROUP BY ms1.Member
) rels ON rels.Sponsee_Member = mship1.Member
INNER JOIN @membership mship2 ON mship2.Member = rels.Sponsor_Member AND mship2.Club = mship1.Club
INNER JOIN @member m1 ON m1.Id = mship1.Member
INNER JOIN @member m2 ON m2.Id = mship2.Member
LEFT JOIN @sponsorship sship ON sship.Sponsor_Membership = mship2.Id
WHERE sship.Id IS NULL
;
在 the rextester that you provided 中,返回:
Id | Member | Sponsor | Club | Dues
-----|-----------------|--------------|----------------|-----
6 | Rod Roosevelt | John Jones | Knitting Club | 5
创建此查询的工作使我想到可以优化数据库设计。当前的模型将很难保持一致性:您的问题本身就证明了这一点。将来,如果赞助商在他的一位赞助人已经参加的新俱乐部注册,会发生什么?再一次,您将需要检测丢失的sponshorship
关系,并以某种方式创建它。
实际上,赞助商和赞助商之间存在1-1的关系,因为您声明赞助应涵盖所有共享成员身份。看起来您似乎不会允许赞助商拥有多个赞助商,即使在不同的俱乐部也是如此。
我建议您放下sponshorship
表并将直接自外部密钥存储给发起人的成员表中。从这里开始,很容易检查两个成员共有哪些俱乐部,并使用SQL查询正确分配会费。
答案 1 :(得分:0)
我发现我可以通过与CTE收集所有赞助关系,在此基础上找到所有应该赞助的会员身份,然后用EXCEPT删除所有现有赞助,从而在一个查询中收集该信息。我剩下应该存在的赞助,但不存在。
WITH sponsored_relationships AS (
SELECT DISTINCT
sponsee_member.Id AS Sponsee
,sponsor_member.Id AS Sponsor
FROM
@sponsorship AS s
JOIN @membership AS sponsee_mship
ON s.Sponsee_Membership = sponsee_mship.Id
JOIN @member AS sponsee_member
ON sponsee_mship.Member = sponsee_member.Id
JOIN @membership AS sponsor_mship
ON s.Sponsor_Membership = sponsor_mship.Id
JOIN @member AS sponsor_member
ON sponsor_mship.Member = sponsor_member.Id
)
SELECT
see_mem.Name AS Sponsee
,sor_mem.Name AS Sponsor
,c.Name AS Club
FROM
sponsored_relationships AS sr
JOIN @member AS see_mem
ON sr.Sponsee = see_mem.Id
JOIN @membership AS see_mship
ON see_mship.Member = see_mem.Id
JOIN @member AS sor_mem
ON sr.Sponsor = sor_mem.Id
JOIN @membership AS sor_mship
ON sor_mship.Member = sor_mem.Id
JOIN @club AS c
ON (see_mship.Club = c.Id
AND sor_mship.Club = c.Id
)
EXCEPT
SELECT
see_mem.Name AS Sponsee
,sor_mem.Name AS Sponsor
,c.Name AS Club
FROM
sponsored_relationships AS sr
JOIN @member AS see_mem
ON sr.Sponsee = see_mem.Id
JOIN @membership AS see_mship
ON see_mship.Member = see_mem.Id
JOIN @member AS sor_mem
ON sr.Sponsor = sor_mem.Id
JOIN @membership AS sor_mship
ON sor_mship.Member = sor_mem.Id
JOIN @club AS c
ON (see_mship.Club = c.Id
AND sor_mship.Club = c.Id
)
JOIN @sponsorship AS sship
ON (sship.Sponsee_Membership = see_mship.Id
AND sship.Sponsor_Membership = sor_mship.Id
);