【问题标题】:Sql select based on the value in other rowssql 根据其他行中的值进行选择
【发布时间】:2021-07-15 16:17:03
【问题描述】:

我有一张如下表:

DS_no   Value   Language   Nach
1        0        EN        123
2        ABC      EN        123
2        DCEF     EN        123
1        1        EN        122
2        XZU      EN        122
2        SDF      EN        122

所以我希望我的 select 语句检查 DS_no 的值是否每个 nach 都为 1。如果值为 1,那么它应该为该 nach 选择所有其他值。例如。 nach 122 的 ds_no 1。如果 ds_no 1 的值为 0,则选择不应返回任何值。

例如:nach 123 的 ds_no 1 的值为 0。所以我不想选择 nach 123 的任何值。但是 nach 122 的 ds_no 1 的值为 1。所以我想选择 nach 的所有值122 除了值 1。

【问题讨论】:

  • 没有“第一价值”这样的东西。 SQL 表代表 无序 集。排序由列定义。
  • 1 - 想要的结果集是什么? 2 - 最后一个nach 123 是错字吗,应该是nach 122,不是吗?

标签: sql oracle select rows oracle12c


【解决方案1】:

没有 first 值。但是您可以使用 exists 检查 any 值是否为 0

select t.*
from t
where exists (select 1
              from t t2
              where t2.nach = t.nach and t2.value = '0'
             );

【讨论】:

  • 为了更好地理解第一个值,我已经编辑了我的问题。
【解决方案2】:

您发布的数据示例

SQL> select * from test;

     DS_NO VALU       NACH
---------- ---- ----------
         1 0           123
         2 abc         123
         2 dcef        123
         1 1           122
         2 xzu         122
         2 sdf         122

6 rows selected.

查询返回您所描述的内容,并附注您 - 很可能 - 在这里打错了:

所以我想选择除值 1 之外的所有 nach 123 值。

123 应该是122,我猜。

SQL> select ds_no, value, nach
  2  from test a
  3  where value <> '1'
  4    and exists (select null
  5                from test b
  6                where b.nach = a.nach
  7                  and b.value = '1'
  8               );

     DS_NO VALU       NACH
---------- ---- ----------
         2 sdf         122
         2 xzu         122

SQL>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2019-06-28
    相关资源
    最近更新 更多