如果列不不同则不要选择行

时间:2018-10-05 07:43:47

标签: sql oracle

我有一个表phone_numbers
有样本数据:

phone_number, id  
09123456789, 1234  
09876543210, 1234  
09012463579, 5678  

我希望输出为:

phone_number, id  
09012463579, 5678  

having count()子句之后是否可以使用where函数?
select distinct也不起作用

3 个答案:

答案 0 :(得分:2)

似乎您需要条件SELECT SUM(spending) as totSpending FROM militaryspending HAVING SUM(spending) > 200000;

totSpending
1699154.3

或使用存在

count

答案 1 :(得分:0)

    select f.phone_number
          ,f.id
    from ( select t.phone_number
                 ,t.id
                 ,count(*) over( partition by t.id ) as dpl
           from table t ) f
    where f.dpl = 1

答案 2 :(得分:-1)

我相信这是最简单的正确解决方案:

  SELECT MAX (phone_number) as phone_number, id
    FROM phone_numbers
GROUP BY id
  HAVING COUNT (*) = 1

具有测试数据:

WITH phone_numbers AS
(
SELECT '09123456789' AS phone_number , 1234 AS ID FROM dual
UNION ALL
SELECT '09876543210' AS phone_number , 1234 AS ID FROM dual
UNION ALL
SELECT '09012463579' AS phone_number , 5678 AS ID FROM dual
)
  SELECT MAX (phone_number) as phone_number, id
    FROM phone_numbers
GROUP BY id
  HAVING COUNT (*) = 1

phone_number |  id   | 
----------------------
09012463579  |  5678 |

希望我能帮上忙!