仍是VFP9重复记录过滤的有关问题:有10个字段,其中ID最关键,其余的重复与否无所谓。凡ID重复的记录,只保留一条,如何Select

仍是VFP9重复记录过滤的问题:有10个字段,其中ID最关键,其余的重复与否无所谓。凡ID重复的记录,只保留一条,怎么Select?
待整理来自不同地方、不同部门发过来的表,表格大概是这个样子:

C0031001, 沙发, 皮质, 马来西亚, 双人, ...
C0031002, 躺椅, 木质, 广州, 单人,...
C0031003, 餐桌, 木质, 杭州, 四人,...
C0031001, 梳发, 真皮, 马来亚, 双人,...
C0031003, 餐桌, 实木, 杭州,圆桌...
...
...

因为不同的地方,不同的称呼,有些字段会不统一,那无关紧要,看得明白就行,唯独ID字段是统一的,无歧义。现在打算整理成一份汇总表,一个ID只要保留一行记录,其余字段的要求不太严格,象上边的例子,五行记录,只要保留1~3行或者2、4、5行即可。

若按以往VFP6的做法,我会这么弄:
select * from A表 Union select * from B表 Union select * from C表 Union select * from D表 into table 大杂烩表
index on ID uniq
copy to 汇总表

这样就能得到ID字段唯一,其余字段随意的目标表格。
假如改用SQL标准语法(注:不想要那种Sys(3099,70)风格的Select),这个需求,应如何用Select语句来完成?
原因是:经常会遇到类似表格,而且以后万一因种种原因,不能安装使用VFP时,我应该怎么做?

------解决方案--------------------
本帖最后由 dkfdtf 于 2012-11-01 22:30:10 编辑
标准点的方法也是差不多的

Step 1. 合并表, 加一个辅助字段 rec
Select Cast(0 as int) as rec, * from t1 ;
union ;
select 0, * from t2 ;
union ;
...
into cursor ttt readwrite

Step 2. recno() -> rec
Replace all rec with Recno()

Step 3. 删除重复 id
Delete a From ttt a ;
  inner join ( ;
    select id, Min(rec) as rec from ttt group by id having Count(*)>1) b ;
  on a.id = b.id and a.rec <> b.rec

现在 ttt 中只留下唯一 id 的行
------解决方案--------------------
表中没有唯一标识的字段
select *,recno() as bz from tt into curs ss
select * from ss a where not exists(select 1 from ss where a.id=id and a.bz>bz)
------解决方案--------------------
要想用标准的SQL语,你的表字段和需求也要标准。
------解决方案--------------------
对于多个字段不同,一个字段相同,通常
用sql 解决办法,用自增列(autoinc列)来区分排除相同的id记录,步骤
1.创建一比要处理的表多一个自增列字段的临时表
    create cursor tmp (fid1 i autoinc, 其它字段与原表字段相同)
2. insert into tmp (字段列表)
   select ... from 表1 union select ... from 表2
   全部数据放于临时表中 tmp中,得到的数据fid1字段唯一,id字段可能有多个相同
3. 对相同id记录,用not exists 来取最小(或最大)fid字段记录,其它记录放弃
   select * from tmp a where not exists(select * from tmp id=a.id and fid<a.fid)