大家好,
我现在有个表,数据如下:
Tabl
cpha col2 col3 cnum
a1 b1 c1 1
a1 d2 e2 2
a3 c3 f3 5
d4 d2 d1 2
a3 e2 e3 4我想通过一条sql语句来获取下面的数据,规则是根据cpha分组后,取cnum最大的一条。
cpha col2 col3 cnum
a1 d2 e2 2
a3 c3 f3 5
d4 d2 d1 2
我现在有个表,数据如下:
Tabl
cpha col2 col3 cnum
a1 b1 c1 1
a1 d2 e2 2
a3 c3 f3 5
d4 d2 d1 2
a3 e2 e3 4我想通过一条sql语句来获取下面的数据,规则是根据cpha分组后,取cnum最大的一条。
cpha col2 col3 cnum
a1 d2 e2 2
a3 c3 f3 5
d4 d2 d1 2
解决方案 »
- sys_connect_by_path连接的字符串顺序反了?
- 这个存储过程必须是到了当前时间了才会停止吗?
- 如何用sql语句写入含blob类型字段的数据?
- c++ oci
- 请问导入数据能否将原来的覆盖掉,只想存放我导进去的数据
- 我的一个sql语句执行巨慢,现在就是我执行的inner jion语句特别慢。各位大哥帮我分析分析!
- 插入日期列的数据有错误
- 我提过的问题怎么没人回答呀,高手们快看看呀,就是我想在每月的25号自动调用一个存储过程,怎么解决?
- 给兄弟看看这条sql语句!!
- 西域浪子请进,再问一条SQL语句,昨天我问的有问题,今天重新问一下
- WIN2003安装完ORACLE 9i以后,必须手工设置环境变量吗?
- 关于本地管理表空间的存储。。
from tabl
group by cpha,col2,col3
select a.*
from tabl a,
(
select cpha,max(cnum) cnum
from tabl
group by cpha)b
where a.cpha=b.cpha and a.cnum=b.cnum
from Tabl t2,(select cpha,max(cnum) cnum from Tabl) t1
where t1.cpha = t2.cpha and t1.cnum = t2.cnum
insert into mtest(cpha,col2,col3,cnum) values('a1','b1','c1',1);
insert into mtest(cpha,col2,col3,cnum) values('a1','b2','e2',2);
insert into mtest(cpha,col2,col3,cnum) values('a3','c3','f3',5);
insert into mtest(cpha,col2,col3,cnum) values('d4','d2','d1',2);
insert into mtest(cpha,col2,col3,cnum) values('a3','e2','e3',4);to hebo2005 ,你的执行结果有问题的了。
group by cpha) a
换种方式试试select a.*
from tabl a
where exists
(select 'x'
from
(select cpha,max(cnum) cnum
from tabl
group by cpha)b
where a.cpha=b.cpha and a.cnum=b.cnum)
select t.* from
(
select 'a1' cpha,'b1' col2,'c1' col3,'1' cnum from dual union all
select 'a1','d2','e2','2' from dual union all
select 'a3','c3','f3','5' from dual union all
select 'd4','d2','d1','2' from dual union all
select 'a3','e2','e3','4' from dual
) t
where not exists(select 1 from (
select 'a1' cpha,'b1' col2,'c1' col3,'1' cnum from dual union all
select 'a1','d2','e2','2' from dual union all
select 'a3','c3','f3','5' from dual union all
select 'd4','d2','d1','2' from dual union all
select 'a3','e2','e3','4' from dual
) where cpha = t.cpha and cnum > t.cnum)--------------------------------------------------
CPHA COL2 COL3 CNUM
1 a1 d2 e2 2
2 a3 c3 f3 5
3 d4 d2 d1 2
再转一个乌龟的帖子给你,应该能找到你要的(MSSQL的)--按某一字段分组取最大(小)值所在行的数据
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
--七,如果整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 1 a1--a的第一个值
a 3 a3:a的第三个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)drop table tb,tmp/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
goselect m.name,m.val,m.memo from
(
select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)drop table tb/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值(2 行受影响)
*/
(
select * from clxx t
where not exists(select 1 from clxx where cpha = t.cpha and LSTI > t.LSTI)
)a
总共花: 13.297m 数据总数:679287 (与别的sql数据总数对不上)
select count(*)
from clxx a,
(
select cpha,max(lsti) cnum
from clxx
group by cpha)b
where a.cpha=b.cpha and a.lsti=b.cnum
总共花:13.578m 数据总数:678933select count(*) from
(
select a.*
from clxx a
where exists
(select 'x'
from
(select cpha,max(LSTI) cnum
from clxx
group by cpha)b
where a.cpha=b.cpha and a.LSTI=b.cnum)
)a
总共花:28.75 数据总数:678933
本身就费时的
(select cpha ,max(cnum) as m from temp group by cpha)a,
temp t
where t.CPHA=a.cpha(+)
and a.m=t.CNUM
SELECT CPHA, COL2, COL3, CNUM
FROM (
SELECT
DENSE_RANK() OVER (PARTITION BY CPHA ORDER BY CNUM DESC) AS SEQ,
MAX(CNUM) OVER (PARTITION BY CPHA ORDER BY CNUM) AS CNUM
,CPHA,COL2,COL3
FROM MTEST
)
WHERE SEQ = 1
SELECT
CPHA,
MAX(COL2) KEEP(DENSE_RANK FIRST ORDER BY CNUM DESC) ,
MAX(COL3) KEEP(DENSE_RANK FIRST ORDER BY CNUM DESC) ,
MAX(CNUM) KEEP(DENSE_RANK FIRST ORDER BY CNUM DESC)
FROM TABL
GROUP BY CPHA
(select cpha, max(cnum) from Tabl group by cpha)
(
select t.*,rank() over(partition by cpha order by cnum desc ) as rankRec from Tabl t
) R
where R.rankRec = 1;
(
select t.*,rank() over(partition by cpha order by cnum desc ) as rankRec from Tabl t
) R
where R.rankRec = 1;
第一:有两行或两行以上的cpha字段值相等时,但cnum不等时,取其cnum字段值最大的一行.
第二:有两行或两行以上的cpha字段值相等时,且cnum相等时,取任意一行(默认取第一行).
第三:取cpha字段值没有相同的,即cpha字段值有且只有一行,那就取这一行.
先创建表:
create table Tabl(cpha varchar(20),col2 varchar(20),col3 varchar(20),cnum integer)
CREATE UNIQUE NONCLUSTERED INDEX [IX_Tabl] ON [dbo].[Tabl]
(
[cpha] ASC,
[cnum] ASC,
[col2] ASC,
[col3] ASC
)WITH (PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
insert into Tabl(cpha,col2,col3,cnum) values('a1','b1','c1',1);
insert into Tabl(cpha,col2,col3,cnum) values('a1','b2','e2',2);
insert into Tabl(cpha,col2,col3,cnum) values('a3','c3','f3',5);
insert into Tabl(cpha,col2,col3,cnum) values('d4','d2','d1',2);
insert into Tabl(cpha,col2,col3,cnum) values('d4','w1','t2',2);
insert into Tabl(cpha,col2,col3,cnum) values('a3','e2','e3',4);
insert into Tabl(cpha,col2,col3,cnum) values('d5','w1','t2',4);
查询语句如下:--第一:取cpha相同,cnum最大的一条
select a.cpha,a.col2,a.col3 ,a.cnum from Tabl a,Tabl b
where (a.cnum>b.cnum and a.cpha=b.cpha)
union
--第二:取cpha相同,cnum相同的第一条(表要约束不能插入相同的行)
select top 1 a.cpha,a.col2,a.col3 ,a.cnum from Tabl a,Tabl b
where (a.cnum=b.cnum and a.cpha=b.cpha and a.col2<>b.col2 and a.col3<>b.col3)
union
--第三:取cpha没有相同的,即有且只有一行cpha字段值
select * from Tabl d
where cpha in
(
select a.cpha from Tabl a
group by a.cpha having count(*)=1
)
望各位多多批评指教!
(SELECT col2 FROM mtest WHERE CPHA=a.CPHA AND CNUM=a.CNUM)col2,
(SELECT col3 FROM mtest WHERE CPHA=a.CPHA AND CNUM=a.CNUM)col3,
a.CNUM
FROM(SELECT CPHA,Max(CNUM)CNUM FROM mtest GROUP BY CPHA)a这就是你想要的了
(SELECT col2 FROM mtest WHERE CPHA=a.CPHA AND CNUM=a.CNUM AND rowNum=1)col2,
(SELECT col3 FROM mtest WHERE CPHA=a.CPHA AND CNUM=a.CNUM AND rowNum=1)col3,
a.CNUM
FROM(SELECT CPHA,Max(CNUM)CNUM FROM mtest GROUP BY CPHA)a如果cpHA与CNUM有重复的话就要加个 AND rowNum=1 这样会确保col2与col3不会取不同行的记录
select * from
(
select cpha,col2,col3,rank()over(partition by cpha order by cnum desc) cnumm from tabname
)where cnumm==1
where cnum in(
select max(cnum) from mtest
group by cpha
)