[英]How can I select a grouped row where it only contains null values in another column?
It's a bit confusing, so I'll try to exemplify in the table below:这有点令人困惑,所以我将尝试在下表中举例说明:
example-table示例表
id ![]() |
data1![]() |
data2![]() |
data3![]() |
---|---|---|---|
1 ![]() |
NULL![]() |
NULL![]() |
NULL![]() |
1 ![]() |
NULL![]() |
NULL![]() |
NULL![]() |
2 ![]() |
1 ![]() |
NULL![]() |
1 ![]() |
2 ![]() |
1 ![]() |
NULL![]() |
NULL![]() |
2 ![]() |
1 ![]() |
NULL![]() |
1 ![]() |
3 ![]() |
1 ![]() |
NULL![]() |
NULL![]() |
3 ![]() |
1 ![]() |
NULL![]() |
1 ![]() |
4 ![]() |
NULL![]() |
NULL![]() |
NULL![]() |
So, grouping by the same ID, I only want to display those IDs that all their data, from all columns, are null.因此,按相同的 ID 分组,我只想显示那些来自所有列的所有数据都为空的 ID。 In the table example above, only IDs 1 and 4.
在上面的表格示例中,只有 ID 1 和 4。
The code I'm trying to use - just for reference:我正在尝试使用的代码 - 仅供参考:
select id
from example-table
group by id, data1, data2, data3
having data1 is null and data2 is null and data3 is null;
Any suggestions?有什么建议?
You can group by id only and set the conditions in the HAVING
clause:您可以仅按 id 分组并在
HAVING
子句中设置条件:
SELECT id
FROM tablename
GROUP BY id
HAVING COALESCE(MAX(data1), MAX(data2), MAX(data3)) IS NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.