需要一个SQL SELECT语句才能返回一个列中具有相同ID的行,而在另一列中具有独特的值
我有一个包含组号列和数据列的表:
group | datacolumn |
---|---|
1 | null |
1 | null |
1 | “ hello” |
1 | null |
2 | “ bye” |
3 | 2 “对不起” |
null 3 | null |
3 | null 3 null |
3 | null |
我想返回<<<<<代码> datacolunm 只要该组中的所有行都包含一个字符串(无行为null)。
如果组中的任何一行为null,则我想在 dataColumn
中返回该组中的所有行。
我所需的输出是:
组 | dataColumn |
---|---|
1 | null |
1 null 1 | null |
1 | null(swap“ hello”至null,因为组1的另一个值是null) |
1 | null |
2 | “ bye” |
2 | “对不起” |
3 | null |
3 null 3 null 3 null 3 | null |
3 | null |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
count()
窗口函数来计数每个group
的所有行,并将结果与具有非零值的行数进行比较:请参阅“ norefollow noreferrer”> demo
Use
COUNT()
window function to count all the rows of eachGROUP
and compare the result to the number of the rows with non-null values:See the demo.
这是一个选项:检查
null
和不是每个组的
值是一个正数;如果是这样,请返回该组的null
。示例数据:
查询从这里开始:
Here's one option: check whether number of
null
andnot null
values per each group is a positive number; if so, returnnull
for that group.Sample data:
Query begins here: