需要一个SQL SELECT语句才能返回一个列中具有相同ID的行,而在另一列中具有独特的值

发布于 2025-02-12 18:15:41 字数 1176 浏览 1 评论 0 原文

我有一个包含组号列和数据列的表:

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

I have a table that contains a group number column and a data column:

GROUP DataColumn
1 NULL
1 NULL
1 "hello"
1 NULL
2 "bye"
2 "sorry"
3 NULL
3 NULL
3 NULL

I want to return the string in the DataColunm as long as all rows in that group contain a string (no row is null).

If any row in the group is NULL then I'd like to return all rows in that group with NULL in the DataColumn.

My desired output would be:

GROUP DataColumn
1 NULL
1 NULL
1 NULL (swap "hello" to null since the other values for group 1 are null)
1 NULL
2 "bye"
2 "sorry"
3 NULL
3 NULL
3 NULL

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

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

发布评论

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

评论(2

ㄟ。诗瑗 2025-02-19 18:15:41

使用 count()窗口函数来计数每个 group 的所有行,并将结果与​​具有非零值的行数进行比较:

SELECT "GROUP",
       CASE 
         WHEN COUNT(*) OVER (PARTITION BY "GROUP") = 
              COUNT("DataColumn") OVER (PARTITION BY "GROUP") 
            THEN "DataColumn" 
       END "DataColumn"
FROM tablename;

请参阅“ norefollow noreferrer”> demo

Use COUNT() window function to count all the rows of each GROUP and compare the result to the number of the rows with non-null values:

SELECT "GROUP",
       CASE 
         WHEN COUNT(*) OVER (PARTITION BY "GROUP") = 
              COUNT("DataColumn") OVER (PARTITION BY "GROUP") 
            THEN "DataColumn" 
       END "DataColumn"
FROM tablename;

See the demo.

葬花如无物 2025-02-19 18:15:41

这是一个选项:检查 null 不是每个组的值是一个正数;如果是这样,请返回该组的 null

示例数据:

SQL> set null NULL
SQL> with test (cgroup, datacolumn) as
  2    (select 1, null    from dual union all
  3     select 1, null    from dual union all
  4     select 1, 'hello' from dual union all
  5     select 1, null    from dual union all
  6     select 2, 'bye'   from dual union all
  7     select 2, 'sorry' from dual union all
  8     select 3, null    from dual union all
  9     select 3, null    from dual union all
 10     select 3, null    from dual
 11    ),

查询从这里开始:

 12  temp as
 13    (select cgroup, datacolumn,
 14       sum(case when datacolumn is null then 1 else 0 end) over (partition by cgroup) cnt_null,
 15       sum(case when datacolumn is null then 0 else 1 end) over (partition by cgroup) cnt_not_null
 16     from test
 17    )
 18  select cgroup,
 19    case when cnt_null > 0 and cnt_not_null > 0 then null
 20         else datacolumn
 21    end as datacolumn
 22  from temp;

    CGROUP DATACOLUMN
---------- ---------------
         1 NULL
         1 NULL
         1 NULL
         1 NULL
         2 bye
         2 sorry
         3 NULL
         3 NULL
         3 NULL

9 rows selected.

SQL>

Here's one option: check whether number of null and not null values per each group is a positive number; if so, return null for that group.

Sample data:

SQL> set null NULL
SQL> with test (cgroup, datacolumn) as
  2    (select 1, null    from dual union all
  3     select 1, null    from dual union all
  4     select 1, 'hello' from dual union all
  5     select 1, null    from dual union all
  6     select 2, 'bye'   from dual union all
  7     select 2, 'sorry' from dual union all
  8     select 3, null    from dual union all
  9     select 3, null    from dual union all
 10     select 3, null    from dual
 11    ),

Query begins here:

 12  temp as
 13    (select cgroup, datacolumn,
 14       sum(case when datacolumn is null then 1 else 0 end) over (partition by cgroup) cnt_null,
 15       sum(case when datacolumn is null then 0 else 1 end) over (partition by cgroup) cnt_not_null
 16     from test
 17    )
 18  select cgroup,
 19    case when cnt_null > 0 and cnt_not_null > 0 then null
 20         else datacolumn
 21    end as datacolumn
 22  from temp;

    CGROUP DATACOLUMN
---------- ---------------
         1 NULL
         1 NULL
         1 NULL
         1 NULL
         2 bye
         2 sorry
         3 NULL
         3 NULL
         3 NULL

9 rows selected.

SQL>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文