SQL Server 2000 - 如何在查询的最终结果中轮换联接结果? (2+结果)

发布于 2024-10-11 20:53:39 字数 1503 浏览 2 评论 0原文

我问了一个几乎相同的问题并得到了有效的工作答案:

上一个问题

这里有一个微小的变化,使得该问题的答案在我的数据库中的特殊情况下不起作用。在所有情况下,除了一个之外,TableB 中最多只有 2 个可能的条目,正如我在上一篇文章中所说的那样。我的问题是我有一个案例,这并不成立;我需要处理 TableB 中最多 5 个项目。 - 抱歉,如果我知道的话,我会把它添加到原始帖子中。

我不知道如何修改我现在遇到的 2+ 项情况的先前答案,因为它使用 MIN 和 MAX 来解决原始问题。

这将在 SQL Server 2000 服务器上使用。

信息列的顺序根本不重要。

表A

ID | Name
---+------
 1 | John
 2 | Jane
 3 | Bob
 4 | Doug
 5 | Smith

表B

ID | NameID | Information
---+--------+------------
 1 |    1   | Apples
 2 |    1   | Apples
 3 |    2   | Pears
 4 |    2   | Grapes
 5 |    3   | Kiwi
 6 |    5   | Apples
 7 |    5   | Pears
 8 |    5   | Grapes
 9 |    5   | Kiwi
 10|    5   | Kumkwat

期望结果

ID | Name | InformationA | InformationB | InformationC | InformationD | InformationE
---+------+--------------+--------------+--------------+--------------+-------------
 1 | John | Apples       | Apples       | NULL         | NULL         | NULL         
 2 | Jane | Pears        | Grapes       | NULL         | NULL         | NULL         
 3 | Bob  | Kiwi         | NULL         | NULL         | NULL         | NULL         
 4 | Doug | NULL         | NULL         | NULL         | NULL         | NULL         
 5 | Smith| Apples       | Pears        | Grapes       | Kiwi         | Kumkwat

I asked a nearly identical question and got a valid working answer:

Previous Question

There is a minor change here that makes the answer in that question not work in a special case in my database. In all situations expect one there would only be at most 2 possible entries in TableB as I stated this in the previous post. My issue is I have one case where this does not hold true; I need to handle up to 5 items in TableB. - Sorry i would have added it to the original post if I has known.

I do not see how to modify the previous answer for the 2+ items situation I'm running into now as it uses MIN and MAX to solve the original problem.

This will be used on a SQL Server 2000 server.

The order of the Information columns does not matter at all.

TableA

ID | Name
---+------
 1 | John
 2 | Jane
 3 | Bob
 4 | Doug
 5 | Smith

TableB

ID | NameID | Information
---+--------+------------
 1 |    1   | Apples
 2 |    1   | Apples
 3 |    2   | Pears
 4 |    2   | Grapes
 5 |    3   | Kiwi
 6 |    5   | Apples
 7 |    5   | Pears
 8 |    5   | Grapes
 9 |    5   | Kiwi
 10|    5   | Kumkwat

Desired Result

ID | Name | InformationA | InformationB | InformationC | InformationD | InformationE
---+------+--------------+--------------+--------------+--------------+-------------
 1 | John | Apples       | Apples       | NULL         | NULL         | NULL         
 2 | Jane | Pears        | Grapes       | NULL         | NULL         | NULL         
 3 | Bob  | Kiwi         | NULL         | NULL         | NULL         | NULL         
 4 | Doug | NULL         | NULL         | NULL         | NULL         | NULL         
 5 | Smith| Apples       | Pears        | Grapes       | Kiwi         | Kumkwat

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

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

发布评论

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

评论(1

梦行七里 2024-10-18 20:53:39

如果 row_number 可用,这会更容易/更高效!

SELECT Id,
       Name,
       MAX (CASE WHEN idx=0 THEN Information END) AS InformationA ,
       MAX (CASE WHEN idx=1 THEN Information END) AS InformationB ,
       MAX (CASE WHEN idx=2 THEN Information END) AS InformationC ,
       MAX (CASE WHEN idx=3 THEN Information END) AS InformationD ,
       MAX (CASE WHEN idx=4 THEN Information END) AS InformationE 
FROM   (SELECT a.Id,
               a.Name,
               b.Information,
               COUNT(b2.Id) idx
        FROM   TableA a
               LEFT JOIN TableB b
                 ON a.Id = b.NameId
               LEFT JOIN TableB b2
                 on b.NameId = b2.NameId
                    and b2.Id < b.Id
        GROUP  BY a.Id,
                  a.Name,
                  b.Id,
                  b.Information) derived
GROUP  BY Id,
          Name  

This would be much easier/more efficient if row_number were available!

SELECT Id,
       Name,
       MAX (CASE WHEN idx=0 THEN Information END) AS InformationA ,
       MAX (CASE WHEN idx=1 THEN Information END) AS InformationB ,
       MAX (CASE WHEN idx=2 THEN Information END) AS InformationC ,
       MAX (CASE WHEN idx=3 THEN Information END) AS InformationD ,
       MAX (CASE WHEN idx=4 THEN Information END) AS InformationE 
FROM   (SELECT a.Id,
               a.Name,
               b.Information,
               COUNT(b2.Id) idx
        FROM   TableA a
               LEFT JOIN TableB b
                 ON a.Id = b.NameId
               LEFT JOIN TableB b2
                 on b.NameId = b2.NameId
                    and b2.Id < b.Id
        GROUP  BY a.Id,
                  a.Name,
                  b.Id,
                  b.Information) derived
GROUP  BY Id,
          Name  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文