日期:2014-05-17  浏览次数:20692 次

一个简单的问题没答上来,被鄙视了
表A是一个缓慢变化维度,记录了用户在不同时期所在的城市,现在需要把换过城市的用户,城市列出,怎么实现。
ID       City   FromDate               ToDate
001     BJ       2003/03/01           2008/01/01
001     SH       2008/01/02          
002     BJ       2003/03/01           2008/01/01
002     BJ       2008/01/02          

结果
001   BJ
001   SH

------解决方案--------------------
select id,city from 表A group by id,city having count(1)>1;
------解决方案--------------------
select id,city from 表A group by id,city having count(1)>1;
------解决方案--------------------
没有什么的,正常的,什么都精通的不叫人,叫神........

------解决方案--------------------
上面有误,这个
SQL code
with temp as(
  select '001' id, 'BJ' city from dual union all
  select '001' id, 'SH' city from dual union all
  select '002' id, 'BJ' city from dual union all
  select '002' id, 'BJ' city from dual)
SELECT id, city
  FROM temp
 WHERE id IN (SELECT id FROM (SELECT DISTINCT id, city FROM temp) GROUP BY id HAVING COUNT(1) > 1);

------解决方案--------------------
select id,city,row_number()over(parition by city order by id) row_num
from a
where row_num >1;
------解决方案--------------------
探讨
上面有误,这个

SQL code
with temp as(
select '001' id, 'BJ' city from dual union all
select '001' id, 'SH' city from dual union all
select '002' id, 'BJ' city from dual union all
select '002'……

------解决方案--------------------
SQL code
--5楼这个不对,改一下
with temp as(
  select '001' id, 'BJ' city from dual union all
  select '001' id, 'SH' city from dual union all
  select '002' id, 'BJ' city from dual union all
  select '002' id, 'BJ' city from dual union all
  select '003' id, 'BJ' city from dual)
SELECT *
  FROM temp a
 WHERE (SELECT COUNT(1)
          FROM temp b
         WHERE b.id = a.id AND
               b.city <> a.city) > 0;

------解决方案--------------------
我只是来接分的~!
------解决方案--------------------
select id,city from 表A
 where id in (select id from 表A group by id,city having count(1)=1);