怎样分组查询3列重复,1列不重复的数据

cx723 2017-10-13 07:48:54
有一张表,有规格、日期、名称、价格这四个字段,怎样才能分组查询出规格、日期、名称这三列数据完全重复,同时价格不重复的数据?
...全文
1325 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cx723 2017-10-17
  • 打赏
  • 举报
回复
ok,已结贴,再次感谢!
碧水幽幽泉 2017-10-17
  • 打赏
  • 举报
回复
这个可以结贴了。
你还有4个帖子也没结,汗!
碧水幽幽泉 2017-10-17
  • 打赏
  • 举报
回复
引用 13 楼 cx723 的回复:
cnt > 1没有用上


select 规格,日期,名称,最小价格,最大价格
from(select distinct 规格,日期,名称,
count(distinct 价格)over(partition by 规格,日期,名称) cnt,
min(价格)over(partition by 规格,日期,名称) 最小价格,
max(价格)over(partition by 规格,日期,名称) 最大价格
from 表)
where cnt > 1;
碧水幽幽泉 2017-10-17
  • 打赏
  • 举报
回复
引用 10 楼 cx723 的回复:
[quote=引用 9 楼 qq646748739 的回复:]

select 规格,日期,名称,价格
from(select distinct 规格,日期,名称,价格,count(distinct 价格)over(partition by 规格,日期,名称) cnt from 表)
where cnt > 1;

试试看!

可以了,还有一个问题请教您,用partition分组后,价格能排序吗,我想取得按这样的分组条件的最大价格和最小价格,直接在partition by 规格,日期,名称后面加order by 价格,不能执行[/quote]
可以做,试试下面的SQL

select 规格,日期,名称,最小价格,最大价格
from(select distinct 规格,日期,名称,
count(distinct 价格)over(partition by 规格,日期,名称) cnt,
row_number()over(partition by 规格,日期,名称 order by 价格) 最小价格,
row_number()over(partition by 规格,日期,名称 order by 价格 desc) 最大价格,
rownum rn
from 表)
where rn = 1;
cx723 2017-10-17
  • 打赏
  • 举报
回复
cnt > 1没有用上
cx723 2017-10-17
  • 打赏
  • 举报
回复
引用 11 楼 qq646748739 的回复:
[quote=引用 10 楼 cx723 的回复:] [quote=引用 9 楼 qq646748739 的回复:]

select 规格,日期,名称,价格
  from(select distinct 规格,日期,名称,价格,count(distinct 价格)over(partition by 规格,日期,名称) cnt from 表)
 where cnt > 1;
试试看!
可以了,还有一个问题请教您,用partition分组后,价格能排序吗,我想取得按这样的分组条件的最大价格和最小价格,直接在partition by 规格,日期,名称后面加order by 价格,不能执行[/quote] 可以做,试试下面的SQL

select 规格,日期,名称,最小价格,最大价格
  from(select distinct 规格,日期,名称,
              count(distinct 价格)over(partition by 规格,日期,名称) cnt,
			  row_number()over(partition by 规格,日期,名称 order by 价格) 最小价格,
			  row_number()over(partition by 规格,日期,名称 order by 价格 desc) 最大价格,
			  rownum rn
	     from 表)
 where rn = 1; 
[/quote] 查询出来结果只有一条数据
cx723 2017-10-16
  • 打赏
  • 举报
回复
通过了,count(distinct 价格)()over,中间多了个()吧,应该是这个原因吧
碧水幽幽泉 2017-10-16
  • 打赏
  • 举报
回复
引用 5 楼 cx723 的回复:
[quote=引用 4 楼 qq646748739 的回复:]
强烈推荐SQL2的写法![/quote
多谢大神,终于找到答案了,想到用ROW_NUMBER()OVER方法,但是不会用count over方法,不过方法一成功了,方法二执行的时候报错,没执行下去

那你把count(distinct 价格)改成count(价格),试试看!
cx723 2017-10-16
  • 打赏
  • 举报
回复
[quote=引用 4 楼 qq646748739 的回复:] 强烈推荐SQL2的写法![/quote 多谢大神,终于找到答案了,想到用ROW_NUMBER()OVER方法,但是不会用count over方法,不过方法一成功了,方法二执行的时候报错,没执行下去
cx723 2017-10-16
  • 打赏
  • 举报
回复
引用 9 楼 qq646748739 的回复:

select 规格,日期,名称,价格
  from(select distinct 规格,日期,名称,价格,count(distinct 价格)over(partition by 规格,日期,名称) cnt from 表)
 where cnt > 1;
试试看!
可以了,还有一个问题请教您,用partition分组后,价格能排序吗,我想取得按这样的分组条件的最大价格和最小价格,直接在partition by 规格,日期,名称后面加order by 价格,不能执行
碧水幽幽泉 2017-10-16
  • 打赏
  • 举报
回复

select 规格,日期,名称,价格
from(select distinct 规格,日期,名称,价格,count(distinct 价格)over(partition by 规格,日期,名称) cnt from 表)
where cnt > 1;

试试看!
cx723 2017-10-16
  • 打赏
  • 举报
回复
引用 4 楼 qq646748739 的回复:
强烈推荐SQL2的写法!


,为什么重复的数据还是能出来呢,应该查询出gg、cgnri、fbdw相同,tbdj不相同的记录才对啊
wandier 2017-10-14
  • 打赏
  • 举报
回复
同时distinct价格
wandier 2017-10-14
  • 打赏
  • 举报
回复
对三个相同的字段group by ,同时进行count,条件是 having count大于1
碧水幽幽泉 2017-10-14
  • 打赏
  • 举报
回复
数据量不大的话,可以使用这个SQL,好理解:

--SQL1
select 规格,日期,名称,价格
  from 表
 where (规格,日期,名称) in(select 规格,日期,名称 
                             from (select 规格,日期,名称,count(distinct 价格) cnt
                                     from 表
                                    group by 规格,日期,名称
                                    having count(distinct 价格) > 1
                                   )
                           );
数据量大的话,使用下面这个SQL,性能更高

--SQL2
select 规格,日期,名称,价格
  from(select 规格,日期,名称,价格,count(distinct 价格)()over(partition by 规格,日期,名称) cnt from 表)
 where cnt > 1;
碧水幽幽泉 2017-10-14
  • 打赏
  • 举报
回复
强烈推荐SQL2的写法!

1,617

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧