select distinct * from 表
解决方案 »
- 自连接查询分组排序,求讲解
- 怎么我没找到报表开发相关的版块啊?
- Oracle可以处理所谓的“分布式事务”吗?了解这方面的高手请赐教!
- 请问谁有比较专业的oracle数据库网站?
- 请教大侠一个sql语句!
- 讨论:有关各种数据库的优缺点(主要讨论Sybase、Oracle和Microsoft三大公司的数据库产品)--------------参与有分
- 菜鸟问题,关于数据表的导入导出问题?
- Oracle在2000 Server 下和linux下有什么区别?
- 有人知道吗?
- 有没有人遇到过DBAStudio不能启动的情况?
- 请看看这个sql语句,在一些机器上有效,在另一些无效,希望指教
- 日文windows里用英文版的pl/sql developer输中文乱码。
select * from #T
drop table #T
devid cityid roadnum metername meapointid
7555 20 1 脉冲表(5) 5
7555 20 2 脉冲表(5) 5
7555 20 3 脉冲表(4) 4
7555 20 4 脉冲表(3) 3
7666 20 1 脉冲表(6) 6
7666 20 2 脉冲表(6) 6
7666 20 3 脉冲表(5) 5
7999 20 1 脉冲表(7) 7
7999 20 2 脉冲表(5) 5
7888 20 1 脉冲表(8) 8其中devid,cityid,roadnum构成主键
如果这样的话,主键没有重复,不够成重复记录
--如果要从后面取一条记录!那主键的值怎么确定??
--如果除主键有其它记录重复,则取其中一条,主键组合roadnum假设取最大值,如下:--创建测试环境
declare @t table(devid varchar(4),cityid int ,roadnum int,metername varchar(10),meapointid int)
insert into @t select '7555',20,1,'脉冲表(5)',5
union all select '7555',20,2,'脉冲表(5)',5
union all select '7555',20,3,'脉冲表(4)',4
union all select '7555',20,4,'脉冲表(3)',3
union all select '7666',20,1,'脉冲表(6)',6
union all select '7666',20,2,'脉冲表(6)',6
union all select '7666',20,3,'脉冲表(5)',5
union all select '7999',20,1,'脉冲表(7)',7
union all select '7999',20,2,'脉冲表(5)',5
union all select '7888',20,1,'脉冲表(8)',8
--查询
select devid=min(devid),
cityid=min(cityid),
roadnum=(select max(roadnum) from @t where metername=a.metername and meapointid=a.meapointid and devid=a.devid),
metername,
meapointid
from @t a
group by devid,metername,meapointid
order by devid
--结果devid cityid roadnum metername meapointid
----- ----------- ----------- ---------- -----------
7555 20 2 脉冲表(5) 5
7555 20 3 脉冲表(4) 4
7555 20 4 脉冲表(3) 3
7666 20 2 脉冲表(6) 6
7666 20 3 脉冲表(5) 5
7888 20 1 脉冲表(8) 8
7999 20 1 脉冲表(7) 7
7999 20 2 脉冲表(5) 5(所影响的行数为 8 行)--不知道是不是要这个结果!
----- ---------- -----------
7555 脉冲表(5) 5
7555 脉冲表(4) 4
7555 脉冲表(3) 3
7666 脉冲表(6) 6
7666 脉冲表(5) 5
7888 脉冲表(8) 8
7999 脉冲表(7) 7
7999 脉冲表(5) 5我只需要这三列,还有就是贴子发错地方了,环境是ORACLE啊
--创建测试环境
declare @t table(devid varchar(4),cityid int ,roadnum int,metername varchar(10),meapointid int)
insert into @t select '7555',20,1,'脉冲表(5)',5
union all select '7555',20,2,'脉冲表(5)',5
union all select '7555',20,3,'脉冲表(4)',4
union all select '7555',20,4,'脉冲表(3)',3
union all select '7666',20,1,'脉冲表(6)',6
union all select '7666',20,2,'脉冲表(6)',6
union all select '7666',20,3,'脉冲表(5)',5
union all select '7999',20,1,'脉冲表(7)',7
union all select '7999',20,2,'脉冲表(5)',5
union all select '7888',20,1,'脉冲表(8)',8
--查询
select devid=min(devid),
metername,
meapointid
from @t a
group by devid,metername,meapointid
order by devid,meapointid desc
--结果
devid metername meapointid
----- ---------- -----------
7555 脉冲表(5) 5
7555 脉冲表(4) 4
7555 脉冲表(3) 3
7666 脉冲表(6) 6
7666 脉冲表(5) 5
7888 脉冲表(8) 8
7999 脉冲表(7) 7
7999 脉冲表(5) 5(所影响的行数为 8 行)
(select max(rowid) from tablename t2
where t1.id=t2.id and t1.name=t2.name)
group by devid,cityid,roadnum
这样不行,楼主我请你吃饭。