一个简单的问题没答上来,被鄙视了

rottenapple 2010-04-14 01:15:20
表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
...全文
100 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
rottenapple 2010-04-14
  • 打赏
  • 举报
回复
xiexie,结贴了
bingdian1900 2010-04-14
  • 打赏
  • 举报
回复
select id,city from 表A
where id in (select id from 表A group by id,city having count(1)=1);
paopaoma1 2010-04-14
  • 打赏
  • 举报
回复
我只是来接分的~!
tangren 2010-04-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jmcai520 的回复:]
引用 4 楼 tangren 的回复:
上面有误,这个

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
……
[/Quote]
with部分是数据,
8楼要简单一些
tangren 2010-04-14
  • 打赏
  • 举报
回复
--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;
jmcai520 2010-04-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tangren 的回复:]
上面有误,这个

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'……
[/Quote]
师兄你这个也整的太复杂了把!
duqiangcise 2010-04-14
  • 打赏
  • 举报
回复
select id,city,row_number()over(parition by city order by id) row_num
from a
where row_num >1;
tangren 2010-04-14
  • 打赏
  • 举报
回复
--或者这个
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 *
FROM temp a
WHERE (SELECT COUNT(1)
FROM temp b
WHERE b.id = a.id AND
b.city = a.city) < 2;
tangren 2010-04-14
  • 打赏
  • 举报
回复
上面有误,这个
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);
iqlife 2010-04-14
  • 打赏
  • 举报
回复
没有什么的,正常的,什么都精通的不叫人,叫神........
Phoenix_99 2010-04-14
  • 打赏
  • 举报
回复
select id,city from 表A group by id,city having count(1)>1;
tangren 2010-04-14
  • 打赏
  • 举报
回复
select id,city from 表A group by id,city having count(1)>1;

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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