sql—选择只有一行的数据的查询

iih3973s  于 2021-08-09  发布在  Java
关注(0)|答案(5)|浏览(726)

我只是有这样的数据表

ID       PHONE_NUMBER          VERSION         VERSION_NAME
   --------------------------------------------------------------
     1       86578900000              0               3.4.2
     2       86578900000              1               3.4.2.1
     3       899589023200             0               3.4.2
     4       878999933336             0               3.4.2
     5       82199987629              0               3.4.2
     6       82199987629              1               3.4.2.1
     7       888729203892             0               3.4.2.1

所以,我希望电话号码的数据只有一行,版本名称是3.4.2,如下所示:

ID       PHONE_NUMBER        
   -----------------------------
     3           899589023200             
     4           878999933336

我运行查询

Select PHONE_NUMBER from TABLE_C where VERSION_NAME = '3.4.2' group by PHONE_NUMBER having count(b.PHONE_NUMBER) = 1

但它并没有像预期的那样起作用

huus2vyu

huus2vyu1#

这里有一个选择:

SQL> with test (id, phone_number, version_name) as
  2    (select 1, 86578900000 , '3.4.2'   from dual union all
  3     select 2, 86578900000 , '3.4.2.1' from dual union all
  4     select 3, 899589023200, '3.4.2'   from dual union all
  5     select 4, 878999933336, '3.4.2'   from dual union all
  6     select 5, 82199987629 , '3.4.2'   from dual union all
  7     select 6, 82199987629 , '3.4.2.1' from dual union all
  8     select 7, 888729203892, '3.4.2.1' from dual
  9    )
 10  select a.id,
 11         a.phone_number
 12  from test a
 13  where a.version_name = '3.4.2'
 14    and a.phone_number in (select b.phone_number
 15                           from test b
 16                           group by b.phone_number
 17                           having count(*) = 1
 18                          );

        ID  PHONE_NUMBER
---------- -------------
         3  899589023200
         4  878999933336

SQL>

第13行中的条件仅获取 3.4.2 版本名,而子查询(第14-18行)确保电话号码只有一个外观。

stszievb

stszievb2#

你可以用 not exists :

select t.*
from t
where not exists (select 1
                  from t t2
                  where t2.phone_number = t.phone_number and
                        t2.version <> '3.4.2'
                 );

您可以直接将此查询描述为:“返回同一电话号码没有其他版本的所有行 '3.4.2' ".

ars1skjm

ars1skjm3#

您可以使用此方法,首先获取表中只有一个外观的电话号码,然后将该数据与3.4.2版中的电话号码进行比较。例如,电话号码899589023200将在PHU计数中成为一行,因为计数为1,然后899589023200也在版本3.4.2中,因此加入后,我们将在输出中获得它。

with test (id, phone_number, version_name) as
     (select 1, 86578900000 , '3.4.2'   union 
      select 2, 86578900000 , '3.4.2.1' union 
      select 3, 899589023200, '3.4.2'   union 
      select 4, 878999933336, '3.4.2'   union 
      select 5, 82199987629 , '3.4.2'   union 
      select 6, 82199987629 , '3.4.2.1' union 
      select 7, 888729203892, '3.4.2.1' 
     )

SELECT t.*
FROM test t
JOIN (SELECT phone_number, COUNT(*) as ph_count
      FROM 
      test t
      GROUP BY phone_number
      HAVING count(*) = 1) ph_cnt ON ph_cnt.phone_number = t.phone_number
WHERE t.version_name like '3.4.2'
b0zn9rqh

b0zn9rqh4#

您也可以使用 WINDOWS 功能如下:

select * from
(select t.*,
       sum(case when version_name = '3.4.2' then 1 end) over (partition by phone_number ) as sm,
       count(1) over (partition by phone_number ) as cnt
from your_table T)
where cnt = 1 and sm = 1
q9yhzks0

q9yhzks05#

尝试此查询它应该可以工作。。。

Select PHONE_NUMBER from TABLE_C where VERSION_NAME = '3.4.2' group by VERSION_NAME
having count(b.PHONE_NUMBER) = 1

相关问题