因为工作原因需要查询出column1一致column2不一致的数据,我自己写了下面这段sql可以实现,但是感觉自己把sql写复杂了,请问这段sql还能再简化吗,我用的是oracle
select b.column1 from (
select column1 from tableName group by column1 having count()>1
) a left join tableName b on a.column1 = b.column1 group by b.column1,b.column2 having count()=1
不一致不就是不相等么?没明白你为什么会想着写个这么复杂的sql
select * from tableName where column1 <>column2
如果有空值
select * from tableName where nvl(column1,'x') <>nvl(column2,'x')
column1一致column2不一致,查询结果只需要对应的column1有哪些值的话
select column1 from tableName group by column1 having count(distinct column2)>1
如果需要同时展示原表所有字段数据,要么用上面这个数据作为匹配条件,要么用开窗函数来实现只对原表进行一次查询
select * from (
select a.*,count(distinct column2) over(partition by column1) ct from tableName a)
where ct>1
select b.column1 tableName where column1='传入的值' group by column1,column2 having count(column2 )=1
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!