--按某一字段分组取最大(小)值所在行的数据 (爱新觉罗.毓华 2007-10-23于浙江杭州) /* 数据如下: 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 top 1 a.*,b.* from ta a ,tb b where a.批次 = b.批次 order by datediff(mi,a.datecol ,b.datecol) asc
别粘贴那么多 不是我需要的 select * from tableA as A where A.批次=批次 and A.time > time 为什么不对?
--单表自联应该这样写吧我也不知道对不对,没测 select * from tableA as A where exists(select 1 from tableA where 批次= a.批次 and A.time > time) 为什么不对?
可能有多个批次 要每一个批次中时间最大的 不用group by 实现
--分组取最大 declare @t table(ID int,dt datetime,QD int) insert into @t select 1,'2008-05-15 12:20:23.967',null insert into @t select 2,'2008-05-15 13:20:23.967',1 insert into @t select 3,'2008-05-15 14:20:23.967',2 insert into @t select 4,'2008-05-15 14:20:23.967',null insert into @t select 5,'2008-05-15 13:20:23.967',4 insert into @t select 6,'2008-05-15 15:20:23.967',1--按dt分组时 select a.* from (select dt, max(ID) ID from @t group by dt) b left join @t a on a.dt=b.dt and a.ID=b.ID--你的按情况修改
运行如下/* --除重复记录 --charry0110 */ --1 所有字段重复 --select distinct * from t --2 部分字段重复 declare @ta table(ID int,date int,Num int) insert into @ta select 1,10,1 insert into @ta select 1,100,1 insert into @ta select 2,10,1 insert into @ta select 2,100,1--得到ID相同记录date最大的三种方法 --(1) select a.* from @ta a,(select id, max(date) date from @ta group by id) b where a.id=b.id and a.date=b.date --(2) select * from @ta a where [date]=(select max(date) from @ta where a.id=id ) --(3) select * from @ta a where not exists ( select 1 from @ta where id=a.id and date>a.Date) ----将(2)扩展其他同例 --取第一条 select a.* from @ta a where date=(select top 1 date from @ta where ID=a.ID) --取最小 select a.* from @ta a where date=(select min(date) date from @ta where ID=a.ID) --取最大 select a.* from @ta a where date=(select max(date) date from @ta where ID=a.ID) --取随机 select a.* from @ta a where date=(select top 1 date from @ta where ID=a.ID order by newid()) --两个表的情况 declare @tb table(ID int) insert into @tb select 1 insert into @tb select 2--取最小 select c.* from (select a.* from @ta a where date=(select min(date) date from @ta where ID=a.ID)) c inner join @tb b on c.id=b.id --取最大 select c.* from (select a.* from @ta a where date=(select max(date) date from @ta where ID=a.ID)) c inner join @tb b on c.id=b.id --取第一条 select c.* from (select a.* from @ta a where date=(select top 1 date from @ta b where ID=a.ID )) c inner join @tb b on c.id=b.id --取随机 select c.* from (select a.* from @ta a where date=(select top 1 date from @ta b where ID=a.ID order by newid())) c inner join @tb b on c.id=b.id------特殊处理 --分组取最大 declare @t table(ID int,dt datetime,QD int) insert into @t select 1,'2008-05-15 12:20:23.967',null insert into @t select 2,'2008-05-15 13:20:23.967',1 insert into @t select 3,'2008-05-15 14:20:23.967',2 insert into @t select 4,'2008-05-15 14:20:23.967',null insert into @t select 5,'2008-05-15 13:20:23.967',4 insert into @t select 6,'2008-05-15 15:20:23.967',1--按dt分组时 select a.* from (select dt, max(ID) ID from @t group by dt) b left join @t a on a.dt=b.dt and a.ID=b.ID--不取null的方法,按小时分组 select * from (select *,datepart(hh,dt) as hh from @t) a where QD=(select isnull(max(QD),0) from @t where datepart(hh,dt)=a.hh)--取null --select datepart(hh,dt) as hh, max(ID) ID from @t group by datepart(hh,dt) --取null的方法,按小时分组 select a.ID,a.hh,isnull(a.QD,0) from (select datepart(hh,dt) as hh, max(ID) ID from @t group by datepart(hh,dt)) b left join (select *,datepart(hh,dt) as hh from @t) a on a.hh=b.hh and a.ID=b.ID--按月日时分组取分组后最大 select a.ID,a.mm,a.dd,a.hh,isnull(a.QD,0) from (select datepart(mm,dt) as mm, datepart(dd,dt) as dd,datepart(hh,dt) as hh, max(ID) ID from @t group by datepart(mm,dt),datepart(dd,dt),datepart(hh,dt)) b left join (select *,datepart(mm,dt) as mm, datepart(dd,dt) as dd,datepart(hh,dt) as hh from @t) a on a.hh=b.hh and a.ID=b.ID
select * from tableA as t where not exists(select 1 from tableA where 批次= t.批次 and t.time < time) 不过这种写法,会存在时间批次都相等且时间最大的记录会选出多个的问题
id 批次 时间 1 A 5.2 2 A 5.1 3 A 5.7 4 B 6.2 5 B 6.4结果3 A 5.7 5 B 6.4
create table a ( id int, 批次 char(1), 时间 varchar(10) ) insert into a select 1, 'A', '5.2' union select 2, 'A', '5.1' union select 3, 'A', '5.7' union select 4, 'B', '6.2' union select 5, 'B', '6.4'select * from a as b where 时间 = (select max(时间) as N'时间' from a where 批次 = b.批次) ORDER BY b.idid 批次 时间 ----------- ---- ---------- 3 A 5.7 5 B 6.4(2 row(s) affected)
(爱新觉罗.毓华 2007-10-23于浙江杭州)
/*
数据如下:
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 行受影响)
*/
from ta a ,tb b
where a.批次 = b.批次
order by datediff(mi,a.datecol ,b.datecol) asc
select * from tableA as A where A.批次=批次 and A.time > time 为什么不对?
select * from tableA as A where exists(select 1 from tableA where 批次= a.批次 and A.time > time) 为什么不对?
--分组取最大
declare @t table(ID int,dt datetime,QD int)
insert into @t select 1,'2008-05-15 12:20:23.967',null
insert into @t select 2,'2008-05-15 13:20:23.967',1
insert into @t select 3,'2008-05-15 14:20:23.967',2
insert into @t select 4,'2008-05-15 14:20:23.967',null
insert into @t select 5,'2008-05-15 13:20:23.967',4
insert into @t select 6,'2008-05-15 15:20:23.967',1--按dt分组时
select a.* from (select dt, max(ID) ID from @t
group by dt) b left join @t a on a.dt=b.dt and a.ID=b.ID--你的按情况修改
--除重复记录
--charry0110
*/
--1 所有字段重复
--select distinct * from t
--2 部分字段重复
declare @ta table(ID int,date int,Num int)
insert into @ta select 1,10,1
insert into @ta select 1,100,1
insert into @ta select 2,10,1
insert into @ta select 2,100,1--得到ID相同记录date最大的三种方法
--(1)
select a.* from @ta a,(select id, max(date) date from @ta
group by id) b where a.id=b.id and a.date=b.date
--(2)
select * from @ta a
where [date]=(select max(date) from @ta where a.id=id )
--(3)
select * from @ta a where not exists (
select 1 from @ta where id=a.id and date>a.Date)
----将(2)扩展其他同例
--取第一条
select a.* from @ta a where date=(select top 1 date from @ta where ID=a.ID)
--取最小
select a.* from @ta a where date=(select min(date) date from @ta where ID=a.ID)
--取最大
select a.* from @ta a where date=(select max(date) date from @ta where ID=a.ID)
--取随机
select a.* from @ta a where date=(select top 1 date from @ta where ID=a.ID order by newid())
--两个表的情况
declare @tb table(ID int)
insert into @tb select 1
insert into @tb select 2--取最小
select c.*
from (select a.* from @ta a where date=(select min(date) date from @ta where ID=a.ID)) c
inner join @tb b on c.id=b.id
--取最大
select c.*
from (select a.* from @ta a where date=(select max(date) date from @ta where ID=a.ID)) c
inner join @tb b on c.id=b.id
--取第一条
select c.*
from (select a.* from @ta a where date=(select top 1 date from @ta b where ID=a.ID )) c
inner join @tb b on c.id=b.id
--取随机
select c.*
from (select a.* from @ta a where date=(select top 1 date from @ta b where ID=a.ID order by newid())) c
inner join @tb b on c.id=b.id------特殊处理
--分组取最大
declare @t table(ID int,dt datetime,QD int)
insert into @t select 1,'2008-05-15 12:20:23.967',null
insert into @t select 2,'2008-05-15 13:20:23.967',1
insert into @t select 3,'2008-05-15 14:20:23.967',2
insert into @t select 4,'2008-05-15 14:20:23.967',null
insert into @t select 5,'2008-05-15 13:20:23.967',4
insert into @t select 6,'2008-05-15 15:20:23.967',1--按dt分组时
select a.* from (select dt, max(ID) ID from @t
group by dt) b left join @t a on a.dt=b.dt and a.ID=b.ID--不取null的方法,按小时分组
select * from (select *,datepart(hh,dt) as hh from @t) a
where QD=(select isnull(max(QD),0) from @t where datepart(hh,dt)=a.hh)--取null
--select datepart(hh,dt) as hh, max(ID) ID from @t group by datepart(hh,dt)
--取null的方法,按小时分组
select a.ID,a.hh,isnull(a.QD,0) from
(select datepart(hh,dt) as hh, max(ID) ID from @t group by datepart(hh,dt)) b left join
(select *,datepart(hh,dt) as hh from @t) a
on a.hh=b.hh and a.ID=b.ID--按月日时分组取分组后最大
select a.ID,a.mm,a.dd,a.hh,isnull(a.QD,0) from
(select datepart(mm,dt) as mm, datepart(dd,dt) as dd,datepart(hh,dt) as hh, max(ID) ID from @t group by datepart(mm,dt),datepart(dd,dt),datepart(hh,dt)) b left join
(select *,datepart(mm,dt) as mm, datepart(dd,dt) as dd,datepart(hh,dt) as hh from @t) a
on a.hh=b.hh and a.ID=b.ID
不过这种写法,会存在时间批次都相等且时间最大的记录会选出多个的问题
1 A 5.2
2 A 5.1
3 A 5.7
4 B 6.2
5 B 6.4结果3 A 5.7
5 B 6.4
(
id int,
批次 char(1),
时间 varchar(10)
)
insert into a
select
1, 'A', '5.2' union select
2, 'A', '5.1' union select
3, 'A', '5.7' union select
4, 'B', '6.2' union select
5, 'B', '6.4'select * from a as b where 时间 = (select max(时间) as N'时间' from a where 批次 = b.批次) ORDER BY b.idid 批次 时间
----------- ---- ----------
3 A 5.7
5 B 6.4(2 row(s) affected)