选择视图中主从关系的前三个详细信息

发布于 2024-09-18 10:04:25 字数 650 浏览 7 评论 0原文

我在一个人与其朋友之间有一个主详细关系:

id name
-- ------
 1 Jones
 2 Smith
 3 Norris

朋友

id personId friendName
-- -------- ----------
 1        1 Alice
 2        1 Bob
 3        1 Charly
 4        1 Deirdre
 5        2 Elenor

一个人可以拥有任意数量的朋友。我想创建一个视图,选择所有人以及它找到的前三个朋友;像这样的事情:

id name   friend1 friend2 friend3
-- ----   ------- ------- -------
 1 Jones  Alice   Bob     Charly
 2 Smith  Elenor  <null>  <null>
 3 Norris <null>  <null>  <null>

我如何使用标准 SQL 来做到这一点? (微软 SQL Server 2005)。

I have a master detail relationship between a person and its friends:

person

id name
-- ------
 1 Jones
 2 Smith
 3 Norris

friends

id personId friendName
-- -------- ----------
 1        1 Alice
 2        1 Bob
 3        1 Charly
 4        1 Deirdre
 5        2 Elenor

A person can have as many friends as he wants. I want to create a view that selects all persons together with the first three friends it finds; something like this:

id name   friend1 friend2 friend3
-- ----   ------- ------- -------
 1 Jones  Alice   Bob     Charly
 2 Smith  Elenor  <null>  <null>
 3 Norris <null>  <null>  <null>

How do I do this with standard SQL? (Microsoft SQL Server 2005).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声无音无过去 2024-09-25 10:04:25
    SELECT p.Id, p.name,
            MAX(CASE RowNum
                WHEN 1 THEN
                 FriendName
                ELSE
                 NULL
             END) Friend1,
            MAX(CASE RowNum
                WHEN 2 THEN
                 FriendName
                ELSE
                 NULL
             END) Friend2,
            MAX(CASE RowNum
                WHEN 3 THEN
                 FriendName
                ELSE
                 NULL
             END) Friend3
     FROM   Person p
     LEFT   JOIN (SELECT id, PersonId, FriendName,
                        ROW_NUMBER() OVER(PARTITION BY PersonId ORDER BY id) RowNum
                 FROM   Friends) f
     ON     f.PersonId = p.Id
    GROUP  BY p.Id, p.Name

结果:

1   Jones   Alice   Bob Charly
3   Norris  NULL    NULL    NULL
2   Smith   Elenor  NULL    NULL
    SELECT p.Id, p.name,
            MAX(CASE RowNum
                WHEN 1 THEN
                 FriendName
                ELSE
                 NULL
             END) Friend1,
            MAX(CASE RowNum
                WHEN 2 THEN
                 FriendName
                ELSE
                 NULL
             END) Friend2,
            MAX(CASE RowNum
                WHEN 3 THEN
                 FriendName
                ELSE
                 NULL
             END) Friend3
     FROM   Person p
     LEFT   JOIN (SELECT id, PersonId, FriendName,
                        ROW_NUMBER() OVER(PARTITION BY PersonId ORDER BY id) RowNum
                 FROM   Friends) f
     ON     f.PersonId = p.Id
    GROUP  BY p.Id, p.Name

result:

1   Jones   Alice   Bob Charly
3   Norris  NULL    NULL    NULL
2   Smith   Elenor  NULL    NULL
治碍 2024-09-25 10:04:25
SELECT t1.id, t1.name,
  (SELECT max(friendname)
   FROM friends t2
   WHERE t2.personid = t1.id)
  "Friend1",
  (SELECT max(friendname)
   FROM friends t2
   WHERE friendname < (SELECT max(friendname)
                       FROM friends t3
                       WHERE t3.personid = t1.id)
   AND t2.personid = t1.id)
  "Friend2",
  (SELECT max(friendname)
   FROM friends t2
   WHERE friendname < (SELECT max(friendname)
                       FROM friends t3
                       WHERE friendname < (SELECT max(friendname)
                                           FROM friends t4
                                           WHERE t4.personid = t1.id)
                       AND t3.personid = t1.id)
   AND t2.personid = t1.id)
  "Friend3"
FROM person t1
SELECT t1.id, t1.name,
  (SELECT max(friendname)
   FROM friends t2
   WHERE t2.personid = t1.id)
  "Friend1",
  (SELECT max(friendname)
   FROM friends t2
   WHERE friendname < (SELECT max(friendname)
                       FROM friends t3
                       WHERE t3.personid = t1.id)
   AND t2.personid = t1.id)
  "Friend2",
  (SELECT max(friendname)
   FROM friends t2
   WHERE friendname < (SELECT max(friendname)
                       FROM friends t3
                       WHERE friendname < (SELECT max(friendname)
                                           FROM friends t4
                                           WHERE t4.personid = t1.id)
                       AND t3.personid = t1.id)
   AND t2.personid = t1.id)
  "Friend3"
FROM person t1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文