使用DISTINCT如何去掉重复记录,并查询所有字段?

zhuxia00 2007-02-06 10:25:58
表(kzw):
id xianlu_id point_name x y h biaod_id
1 19 草桥 314594.654 494252.073
2 19 草桥 314594.654 494252.073
3 19 北宫门 315032.232 492997.054
4 19 北宫门 315032.232 492997.054
5 19 北宫门 315032.232 492997.054 1
6 18 北三环路口 311013.473 496936.988 1
7 18 北三环路口 311013.473 496936.988
8 18 北三环路口 311013.473 496936.988 40.77

筛选结果:

id xianlu_id point_name x y h biaod_id
1 19 草桥 314594.654 494252.073
3 19 北宫门 315032.232 492997.054
5 19 北宫门 315032.232 492997.054 1
6 18 北三环路口 311013.473 496936.988 1
7 18 北三环路口 311013.473 496936.988
8 18 北三环路口 311013.473 496936.988 40.77

数据表规律:
id(自动编号),xianlu_id(数字),point_name(文本),x(文本),y(文本),h(文本),biaod_id(文本)。其中xianlu_id,point_name,x,y,h,biaod_id六个字段唯一确定一个数,为联合主键

实现的效果:
通过xianlu_id,point_name,x,y,h,biaod_id六个字段共同作用,去除重复项,但是要保证能查询出id字段

我的方法:

方法一:利用distinct去除重复项

SELECT distinct point_name,x,y,h,xianlu_id,biaod_id from kzw WHERE xianlu_id = "&xianlu_id&" ORDER BY point_name ASC

结果:
显示记录正确,但是没有办法查询出id字段


方法二:建立子查询

select * from kzw a where not exists(select 1 from kzw where id > a.id and point_name = a.point_name and x= a.x and y = a.y and h = a.h and xianlu_id = a.xianlu_id and biaod_id = a.biaod_id) and xianlu_id = "&xianlu_id&" ORDER BY point_name ASC

结果:
能查询出id字段,但显示记录仍然有很多重复,基本没有筛选

请各位大侠帮小妹指指招,看看是哪个地方出了问题?
...全文
907 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuxia00 2007-02-08
  • 打赏
  • 举报
回复
标准答案:(折腾了几天终于ok了)
SELECT * FROM kzw WHERE id in (select max(id) from kzw group by point_name,x,y,h,xianlu_id,biaod_id) and xianlu_id = "&xianlu_id&" ORDER BY point_name ASC
y_dong119 2007-02-07
  • 打赏
  • 举报
回复
我用过access数据库,里面问题很多,有很多动态的查询条件都查询不出来..
你可以从运用多个sql语句出发,加上程序判断来实现你的目的.
zhuxia00 2007-02-07
  • 打赏
  • 举报
回复
w75251455(砍破)高手,还是记录为空,不行啊

我的方法:
strSQL ="delete kzw where id not in(select min(id)[id] from kzw group by xianlu_id,point_name,x,y,h,biaod_id) select * from kzw where xianlu_id = "&xianlu_id&" ORDER BY point_name ASC"

说明一下:
我是access的数据库,用asp建立数据集,把kzw表里的记录读出来,前面的create table都不能用的
w75251455 2007-02-07
  • 打赏
  • 举报
回复
--这里可是sql版
drop table kzw

create table kzw(id int, xianlu_id int, point_name varchar(10), x decimal(10, 3), y decimal(10, 3), h decimal(10, 2), biaod_id int)
insert kzw select 1, 19, '草桥', 314594.654, 494252.073,null, null
union all select 2, 19, '草桥', 314594.654, 494252.073,null, null
union all select 3, 19, '北宫门', 315032.232, 492997.054,null, null
union all select 4, 19, '北宫门', 315032.232, 492997.054,null, null
union all select 5, 19, '北宫门', 315032.232, 492997.054,null, 1
union all select 6, 18, '北三环路口', 311013.473, 496936.988,null, 1
union all select 7, 18, '北三环路口', 311013.473, 496936.988,null, null
union all select 8, 18, '北三环路口', 311013.473, 496936.988, 40.77, null

delete kzw where id in(select min(id)[id] from kzw group by xianlu_id,point_name,x,y,h,biaod_id having count(1)>1)

select * from kzw
w75251455 2007-02-07
  • 打赏
  • 举报
回复

create table kzw(id int, xianlu_id int, point_name varchar(10), x decimal(10, 3), y decimal(10, 3), h decimal(10, 2), biaod_id int)
insert kzw select 1, 19, '草桥', 314594.654, 494252.073,null, null
union all select 2, 19, '草桥', 314594.654, 494252.073,null, null
union all select 3, 19, '北宫门', 315032.232, 492997.054,null, null
union all select 4, 19, '北宫门', 315032.232, 492997.054,null, null
union all select 5, 19, '北宫门', 315032.232, 492997.054,null, 1
union all select 6, 18, '北三环路口', 311013.473, 496936.988,null, 1
union all select 7, 18, '北三环路口', 311013.473, 496936.988,null, null
union all select 8, 18, '北三环路口', 311013.473, 496936.988, 40.77, null

delete kzw where id not in(select min(id)[id] from kzw group by xianlu_id,point_name,x,y,h,biaod_id)

select * from kzw
zhuxia00 2007-02-07
  • 打赏
  • 举报
回复
marco08(天道酬勤)老大,我试过了,还是不行,我是access的数据库。
我按照你的方法改的查询:
SELECT id=min(id),xianlu_id,point_name,x,y,h,biaod_id from kzw group by xianlu_id,point_name,x,y,h,biaod_id WHERE xianlu_id = "&xianlu_id&" ORDER BY point_name ASC

搜出来的查询结果为空,我怀疑是字段类型的问题。
marco08 2007-02-06
  • 打赏
  • 举报
回复
create table kzw(id int, xianlu_id int, point_name varchar(10), x decimal(10, 3), y decimal(10, 3), h decimal(10, 2), biaod_id int)
insert kzw select 1, 19, '草桥', 314594.654, 494252.073,null, null
union all select 2, 19, '草桥', 314594.654, 494252.073,null, null
union all select 3, 19, '北宫门', 315032.232, 492997.054,null, null
union all select 4, 19, '北宫门', 315032.232, 492997.054,null, null
union all select 5, 19, '北宫门', 315032.232, 492997.054,null, 1
union all select 6, 18, '北三环路口', 311013.473, 496936.988,null, 1
union all select 7, 18, '北三环路口', 311013.473, 496936.988,null, null
union all select 8, 18, '北三环路口', 311013.473, 496936.988, 40.77, null

select id=min(id), xianlu_id, point_name, x, y, h, biaod_id
from kzw
group by xianlu_id, point_name, x, y, h, biaod_id
order by id

--result
id xianlu_id point_name x y h biaod_id
----------- ----------- ---------- ------------ ------------ ------------ -----------
1 19 草桥 314594.654 494252.073 NULL NULL
3 19 北宫门 315032.232 492997.054 NULL NULL
5 19 北宫门 315032.232 492997.054 NULL 1
6 18 北三环路口 311013.473 496936.988 NULL 1
7 18 北三环路口 311013.473 496936.988 NULL NULL
8 18 北三环路口 311013.473 496936.988 40.77 NULL

(6 row(s) affected)
marco08 2007-02-06
  • 打赏
  • 举报
回复
--try


select id=min(id), xianlu_id, point_name, x, y, h, biaod_id
from T
group by xianlu_id, point_name, x, y, h, biaod_id
dongjixing 2007-02-06
  • 打赏
  • 举报
回复
虽然你写了很多,但好象把问题还没说清楚,你想得到什么结果?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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