选择不同的非空值,除非空值是该记录组合的唯一值 (tsql)
我有一张包含学生 ID、服务和提供商的表。我想显示每个服务的不同提供程序,但仅在该服务和 ID 没有其他提供程序时才显示 NULL 提供程序。
换句话说,如果学生有某个提供者和服务,我不想选择提供者为 NULL 的地方,除非该特定学生和提供者没有另一个非 NULL 提供者,在这种情况下我确实想选择NULL 提供者行。我也不希望非空值重复。
这是一个示例表:
ID Service Provider
1 SL Joe
1 SL NULL
2 Sped Mary
2 Sped Jim
2 Sped NULL
2 Sped Mary
3 SL Larry
3 OT NULL
3 SL NULL
我选择的结果是:
ID Service Provider
1 SL Joe
2 Sped Mary
2 Sped Jim
3 SL Larry
3 OT NULL
因此,例如,学生 1 有一个非 NULL 和一个 NULL 服务提供者“SL”,所以我只想显示非 NULL 提供者,乔。学生 2 有四个“Sped”提供者:Mary(两次)、Jim 和 NULL,因此我只想显示 Mary(一次)和 Jim。学生 3 有两次服务“SL”,分别为 Larry 和 NULL,因此我只想显示 Larry。但是,学生 3 的“OT”为 NULL,并且由于该学生/提供者组合没有非 NULL 值,因此我想显示该行的 NULL 值。
该报告旨在向服务提供商展示他们在哪些地方为学生提供了提供商(一件好事),以及哪些学生在没有任何提供商的情况下获得了服务(一件坏事)。我的用户很容易感到困惑,因此我需要将其呈现出来方式。感谢您的帮助!
I have a table with Student ID, Service, and Provider. I want to show the DISTINCT Providers for each Service, but ONLY show NULL Providers if there is no other Provider for that Service and ID.
In other words, if a Student has a certain Provider and Service, I don't want to select where the Provider is NULL, unless that specific Student and Provider do not have another non-NULL Provider, in which case I DO want to select the NULL Provider row. I also don't want duplicates for the non-NULLS.
Here's a sample table:
ID Service Provider
1 SL Joe
1 SL NULL
2 Sped Mary
2 Sped Jim
2 Sped NULL
2 Sped Mary
3 SL Larry
3 OT NULL
3 SL NULL
And what I'd like to get as a result of my select is:
ID Service Provider
1 SL Joe
2 Sped Mary
2 Sped Jim
3 SL Larry
3 OT NULL
So, for example, Student 1 has a non-NULL and a NULL provider for Service "SL," so I just want to show the non-NULL provider, Joe. Student 2 has four "Sped" providers: Mary (twice), Jim, and NULL, so I only want to show Mary (once) and Jim. Student 3 has Service "SL" twice, with Larry and NULL, so I only want to show Larry. However, Student 3 has a NULL for "OT," and since there are no non-NULL values for that Student/Provider combination, I want to show the NULL value for that row.
This report is to show service providers where they've given the student a provider (a good thing) but also where students have a service without any provider (a bad thing.) My users are easily confused, so I need to present it this way. Thank you for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个(在OP说他们在SQL Server 2000之前):
输出:
OP说SQL Server 2000之后的编辑版本:
输出:
try this (before OP said they were on SQL Server 2000):
OUTPUT:
EDIT version after OP said SQL Server 2000:
OUTPUT: