日期:2014-05-17 浏览次数:20831 次
with tmp as ( select to_date('20100302','yyyymmdd') sale_date, '001' employee_id, 'C1' client_id, 'P1' product_id from dual union all select to_date('20100302','yyyymmdd') sale_date, '001' employee_id, 'C1' client_id, 'P2' product_id from dual union all select to_date('20100302','yyyymmdd') sale_date, '002' employee_id, 'C1' client_id, 'P1' product_id from dual union all select to_date('20100302','yyyymmdd') sale_date, '002' employee_id, 'C2' client_id, 'P1' product_id from dual union all select to_date('20100302','yyyymmdd') sale_date, '002' employee_id, 'C2' client_id, 'P2' product_id from dual union all select to_date('20100302','yyyymmdd') sale_date, '002' employee_id, 'C1' client_id, 'P1' product_id from dual ) select mt.sale_date,mt.employee_id,mt.client_id,mt.product_id from tmp mt,( select sale_date,employee_id,client_id,count(distinct product_id) from tmp group by sale_date,employee_id,client_id having count(distinct product_id) > 1 ) st where mt.sale_date = st.sale_date and mt.employee_id = st.employee_id and mt.client_id = st.client_id;
------解决方案--------------------
select *
from A
where (sale_date, employee_id,client_id) in
(select sale_date, employee_id,client_id
from A
group by sale_date, employee_id,client_id
having count(*)>1)
------解决方案--------------------
分区查?over是什么意思?
不懂,帮顶 我也想知道如何弄...
------解决方案--------------------
1.2.3楼都对!
但是1楼好!效率要高些!
------解决方案--------------------
create table a( sale_date date, employee_id char(4), client_id char(4), product_id char(4) ); insert into a(sale_date,employee_id,client_id, product_id) values(to_date('20100302','yyyymmdd'), '001', 'C1', 'P1'); insert into a(sale_date,employee_id,client_id, product_id) values(to_date('20100302','yyyymmdd'), '001', 'C1', 'P2'); insert into a(sale_date,employee_id,client_id, product_id) values(to_date('20100302','yyyymmdd'), '002', 'C1', 'P1'); insert into a(sale_date,employee_id,client_id, product_id) values(to_date('20100302','yyyymmdd'), '002', 'C2', 'P1'); insert into a(sale_date,employee_id,client_id, product_id) values(to_date('20100302','yyyymmdd'), '002', 'C2', 'P2'); insert into a(sale_date,employee_id,client_id, product_id) values(to_date('20100302','yyyymmdd'), '002', 'C1', 'P1'); SELECT to_char(a1.sale_date,'yyyymmdd'), a1.employee_id, a1.client_id, a1.product_id, count(a1.product_id) product_count FROM a a1 GROUP BY to_char(a1.sale_date,'yyyymmdd'), a1.employee_id, a1.client_id, a1.product_id HAVING COUNT(a1.product_id)>=2;
------解决方案--------------------
SQL> edi 已写入 file afiedt.buf 1 select to_char(sale_date,'yyyymmdd') sale_date,employee_id,client_id,product_id from 2 tb a where exists(select 1 from tb where employee_id=a.employee_id and client_id=a.client_id 3 group by employee_id,client_id 4* having count(1)>=2) SQL> / SALE_DAT EMPLO CLIEN PRODU -------- ----- ----- ----- 2010