SQL Server 2000 - 如何在查询的最终结果中轮换联接结果? (2+结果)
我问了一个几乎相同的问题并得到了有效的工作答案:
这里有一个微小的变化,使得该问题的答案在我的数据库中的特殊情况下不起作用。在所有情况下,除了一个之外,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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
row_number
可用,这会更容易/更高效!This would be much easier/more efficient if
row_number
were available!