我正在尝试
select columns Age, Height, House_number, Street
from my_table
where count(combination of House_number, Street)
occurs more than once.
我的 table 看起来像这样
Age, Height, House_number, Street
15 178 6 Mc Gill Crst
85 166 6 Mc Gill Crst
85 166 195 Mc Gill Crst
18 151 99 Moon Street
52 189 14a Grimm Lane
我想要的结果是这样的
Age, Height, House_number, Street
15 178 6 Mc Gill Crst
85 166 6 Mc Gill Crst
卡住!
最佳答案
最好的方法是使用窗口函数,假设您的数据库支持它们:
select columns Age, Height, House_number, Street
from (select t.*, count(*) over (partition by house_number, street) as cnt
from my_table t
) t
where cnt > 1
这是在 Oracle 中使用 windows 函数(也称为分析函数)。表达式
count(*) over (partition by house_number, street)
计算每个 house_number 和街道组合的行数。这有点像执行 group by
,但它将计数添加到每一行,而不是将多行合并为一行。一旦你有了它,就很容易简单地选择值大于 1 的行。
关于SQL 查找只有 2 列值的整行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15013928/