我有下表A
Table A
ID Date Price
123 4/1/2015 300
123 4/1/2015 500
456 4/1/2015 200
456 5/1/2015 200
789 6/1/2015 300
368 NULL 700
场景:我想拉出日期相同但价格不同的所有记录:
例如:
ID Date Price
123 4/1/2015 300
123 4/1/2015 500
答案 0 :(得分:1)
select t1.*
from your_table t1
join
(
select id
from your_table
group by id, date
having count(distinct price) > 1
) t2 on t1.id = t2.id
答案 1 :(得分:0)
一种方法使用分析功能:
select t.*
from (select t.*, min(price) over (partition by id, date) as minprice,
max(price) over (partition by id, date) as maxprice
from t
) t
where minprice <> maxprice;
另一个使用简单的exists
:
select t.*
from t t
where exists (select 1 from t t2 where t2.id = t.id and t2.date = t.date);
这两种方法都假设date
没有时间组件。如果是,则使用trunc(date)
或类似的逻辑。