请教一个SQL语句关于查询显示超过指定数量的明细

xjieloving 2018-05-17 06:10:54
现在有两个表A和B
A(客户编号,次数,商品,经办人,时间,数量)—— 客户编号和次数会重复出现 就是客户一次可以拿多种商品
B(商品,标记) 标记为1或者0

A 、B表用过商品关联 表中其他不重要的字段省略了

我要查询指定时间内在A表中同一客户编号同一次数超过三种在B表中标记为1的数据明细

求方法
...全文
2596 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjieloving 2018-08-15
  • 打赏
  • 举报
回复
先通过AB表查询出来需要的数据(通过B表的标记)
再对上面结果进行汇总求和>3
理清思路,弄清逻辑再写就简单了
饺子没有子 2018-06-12
  • 打赏
  • 举报
回复
select * from tab a,tab b where a.商品=b.商品 and b.标记=1 and a.客户编号 in ( select 客户编号,次数,count(次数) from tab a group by客户编号,次数 having count() >3 )
nayi_224 2018-05-31
  • 打赏
  • 举报
回复
with a as ( select 1 id, 1 ci_shu, 10 shang_pin from dual union all select 1 id, 2 ci_shu, 10 from dual union all select 1 id, 2 ci_shu, 10 from dual union all select 1 id, 2 ci_shu, 10 from dual union all select 1 id, 2 ci_shu, 10 from dual union all select 1 id, 1 ci_shu, 20 from dual union all select 1 id, 1 ci_shu, 30 from dual union all select 1 id, 1 ci_shu, 40 from dual union all select 1 id, 1 ci_shu, 20 from dual union all select 1 id, 3 ci_shu, 30 from dual union all select 2 id, 3 ci_shu, 10 from dual ), b as ( select 10 shang_pin, 1 state from dual union all select 20 shang_pin, 1 from dual union all select 30 shang_pin, 1 from dual union all select 40 shang_pin, 1 from dual union all select 50 shang_pin, 1 from dual ) select*from ( select a.*, count(distinct a.shang_pin) over(partition by a.id, a.ci_shu) ct from a, b where 1 = 1 and a.shang_pin = b.shang_pin and b.state = 1 and 'time' = 'time') t1 where t1.ct > 3 ;
yan_jg 2018-05-18
  • 打赏
  • 举报
回复
刚刚内个有点问题, SELECT * FROM A t WHERE (SELECT COUNT(1) FROM A k WHERE t. 客户编号 = k.客户编号 AND t.次数 = k.次数 AND EXISTS (SELECT 1 FROM B WHERE B.商品 = K.商品 AND B.标识 = 0) AND K.时间 BETWEEN 开始时间 AND 结束时间 GROUP BY 客户编号,次数) > 3 AND EXISTS (SELECT 1 FROM B WHERE B.商品 = T.商品 AND B.标识 = 0) AND T.时间 BETWEEN 开始时间 AND 结束时间
yan_jg 2018-05-18
  • 打赏
  • 举报
回复
给你个蠢方法 你可以先用着 等大神有答案一起学习吧.. SELECT * FROM A t WHERE (SELECT COUNT(1) FROM A k WHERE t. 客户编号 = k.客户编号 AND t.次数 = k.次数 EXISTS (SELECT 1 FROM B WHERE B.商品 = A.商品 AND B.标识 = 0) AND A.时间 BETWEEN 开始时间 AND 结束时间 GROUP BY 客户编号,次数) > 3 AND EXISTS (SELECT 1 FROM B WHERE B.商品 = A.商品 AND B.标识 = 0) AND A.时间 BETWEEN 开始时间 AND 结束时间
xjieloving 2018-05-17
  • 打赏
  • 举报
回复
引用 3 楼 baidu_36457652 的回复:
[quote=引用 2 楼 xjieloving的回复:][quote=引用 1 楼 baidu_36457652 的回复:] select a.客户编号,a.次数 .count(*) as nn from a,b where a.商品=b.商品 and b.标记=1 group by a.客户编号,a.次数 having count(*)>3
关键是我还要显示A表的其他字段信息 这样group by 不行呀 单纯这样我知道这样写[/quote] 那用row_number()over(),然后取序号大于3的[/quote] 没用过 能否举个例子
xjieloving 2018-05-17
  • 打赏
  • 举报
回复
引用 3 楼 baidu_36457652 的回复:
[quote=引用 2 楼 xjieloving的回复:][quote=引用 1 楼 baidu_36457652 的回复:] select a.客户编号,a.次数 .count(*) as nn from a,b where a.商品=b.商品 and b.标记=1 group by a.客户编号,a.次数 having count(*)>3
关键是我还要显示A表的其他字段信息 这样group by 不行呀 单纯这样我知道这样写[/quote] 那用row_number()over(),然后取序号大于3的[/quote] 我百度了用法 发现不满足我的需求 因为我需要的是 超过三条的我都要显示出来少于三条的我直接过滤掉,您这样就只会显示超过三条的那些数据123就不显示了
  • 打赏
  • 举报
回复
引用 2 楼 xjieloving的回复:
[quote=引用 1 楼 baidu_36457652 的回复:] select a.客户编号,a.次数 .count(*) as nn from a,b where a.商品=b.商品 and b.标记=1 group by a.客户编号,a.次数 having count(*)>3
关键是我还要显示A表的其他字段信息 这样group by 不行呀 单纯这样我知道这样写[/quote] 那用row_number()over(),然后取序号大于3的
xjieloving 2018-05-17
  • 打赏
  • 举报
回复
引用 1 楼 baidu_36457652 的回复:
select a.客户编号,a.次数 .count(*) as nn from a,b where a.商品=b.商品 and b.标记=1 group by a.客户编号,a.次数 having count(*)>3
关键是我还要显示A表的其他字段信息 这样group by 不行呀 单纯这样我知道这样写
  • 打赏
  • 举报
回复
select a.客户编号,a.次数 .count(*) as nn from a,b where a.商品=b.商品 and b.标记=1 group by a.客户编号,a.次数 having count(*)>3

17,377

社区成员

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

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