现在有一张t_area表
字段有:
时间 地市编码 地市名称 区县编码 区县名称 网格id 网格名称
data city cityname county countyname groid grodname
然后每天会入30多万条数据进来,但每天数据大多相同,少数不同,我要怎么查找今天和昨天不同的数据
数据是否相同根据什么字段来判断?如根据groid 判断。
select * from t_area where TO_CHAR(data,'yyyy-MM-dd') = TO_CHAR(sysdate,'yyyy-MM-dd') and groid not in(
select groid from t_area where TO_CHAR(data,'yyyy-MM-dd') = TO_CHAR(sysdate-1,'yyyy-MM-dd')
)
看看是不是这个意思:
这个表里的时间字段,就是插入数据时的日期,相当于在这个表里,每日都有一份全量数据,现在你想比较相邻的两天,第二天相较于第一天,数据有些什么区别,可能是有增加,也可能是有减少,也有可能是修改了部分字段的值,你想把这些数据都提取出来,最好是能生成对应的对前一天数据的操作指令?
这玩意是个接口表吧?你是不是想获取变更情况来处理对应的正式表?这里需要考虑一个问题,是否会存在要删除的情况,即某天存在的一条数据,在第二天不存在。因为这会影响到处理方案。如果不删,对着原表直接一个merge into 就完事了;如果要删或者是打失效标记,就得再附加一个操作了
假定正式表名为area
merge into area a
using (select dt, city, cityname, county, countyname, groid, grodname
from t_area
where dt = trunc(sysdate)) b
on (a.groid = b.groid and (
nvl(a.city, 'x') <> nvl(b.city, 'x') or
nvl(a.cityname, 'x') <> nvl(b.cityname, 'x') or
nvl(a.county, 'x') <> nvl(b.county, 'x') or
nvl(a.countyname, 'x') <> nvl(b.countyname, 'x') or
nvl(a.grodname, 'x') <> nvl(b.grodname, 'x')))
when MATCHED then
update
set a.dt = b.dt,
a.city = b.city,
a.cityname = b.cityname,
a.county = b.county,
a.countyname = b.countyname,
a.grodname = b.grodname
when not MATCHED then
insert into
(dt, city, cityname, county, countyname, groid, grodname)
values
(b.dt, b.city, b.cityname, b.county, b.countyname, b.groid, b.grodname);
如果只是要查出不一样的数据,用下面这个
select *
from t_area b
where dt = trunc(sysdate)
and (exists
(select 1
from area a
where a.groid = b.groid
and (nvl(a.city, 'x') <> nvl(b.city, 'x') or
nvl(a.cityname, 'x') <> nvl(b.cityname, 'x') or
nvl(a.county, 'x') <> nvl(b.county, 'x') or
nvl(a.countyname, 'x') <> nvl(b.countyname, 'x') or
nvl(a.grodname, 'x') <> nvl(b.grodname, 'x'))) or not exists
(select 1 from area a where a.groid = b.groid))
要找少了的数据的话,原表not exists 接口表就好了