我正在尝试查询地址表,以检查每个所有者是否重复了ID。
我的基本查询显示了与媒体相关的地址媒体和所有者。
owner_obj_id addr_medium_id is_std_addr
12345 5001 +
12345 5002 +
12345 5001 null
12346 5001 null
我想查询每个不同的owner_obj_id并检查它们是否具有重复的介质ID,例如,它们不应该有5001两次。
答案 0 :(得分:1)
要获得重复组合,您可以执行
select owner_obj_id, addr_medium_id
from tablename
group by owner_obj_id, addr_medium_id
having count(*) > 1
编辑:
select owner_obj_id, addr_medium_id
from tablename
where is_std_addr is not null
group by owner_obj_id, addr_medium_id
having count(*) > 1
答案 1 :(得分:0)
试试这个。使用before(:each) do
@school = build(:school)
@teacher = build(:teacher)
build_list(:mock_with_proccessed_statistics, 2, school: @school, teacher: @teacher)
@mocks = Mock.page(nil)
@q = Mock.ransack
allow(view).to receive(:current_school).and_return(@school)
allow(view).to receive(:format_date) { |date, format| date.strftime(format) }
end
汇总+ Count
声明
Case
答案 2 :(得分:0)
如果您希望拥有多个地址的所有者:
select owner_obj_id
from tablename
group by owner_obj_id
having min(addr_medium_id) <> max(addr_medium_id);
如果您想要原始行,可以使用join
或使用窗口函数执行类似的操作。也许最简单的只是in
:
select t.*
from tablename t
where t.owner_obj_id in (select owner_obj_id
from tablename
group by owner_obj_id
having min(addr_medium_id) <> max(addr_medium_id)
);