﻿ （转）mysql分组取最大(最小、最新、前N条)条记录_彩民之家高手论坛

# （转）mysql分组取最大(最小、最新、前N条)条记录

2019-09-20 06:10 来源：未知

6、依据Name分组取最小的两个(N个)Val

sql数据库达成分组并取每组的前1(几)条数据

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

select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name

3、依据Name分组取第二次面世的行所在的数据。

select a.* from Test_orderByOrGroupBy_tb a where a.Val = (select top 1 Val from Test_orderByOrGroupBy_tb where Name = a.Name order by newid()) order by a.Name
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name

5、依照Name分组取最大的七个(N个)Val

sql如下：

CREATE table Test_orderByOrGroupBy_tb(Name nvarchar(50),Val int,Describe nvarchar(50))
insert into Test_orderByOrGroupBy_tb values('a', 1, 'a1--a的第一个值')
insert into Test_orderByOrGroupBy_tb values('b', 2, 'b2b2b2b2b2b2b2b2b值')
insert into Test_orderByOrGroupBy_tb values('a', 2, 'a2(a的第二个值)')
insert into Test_orderByOrGroupBy_tb values('b', 1, 'b1--b的第一个值')
insert into Test_orderByOrGroupBy_tb values('a', 3, 'a3:a的第三个值')
insert into Test_orderByOrGroupBy_tb values('b', 3, 'b3:b的第三个值')
insert into Test_orderByOrGroupBy_tb values('c', 1, 'c1c1c1c1c1c1c1c1c1c1c值')
insert into Test_orderByOrGroupBy_tb values('b', 5, 'b5b5b5b5b5b5b5b5b5b5值')
insert into Test_orderByOrGroupBy_tb values('c', 2, 'c2c2c2c2c2c2c2c2c2c2值')
insert into Test_orderByOrGroupBy_tb values('b', 4, 'b4b4b4b4b4b4b4b4b值')
GO

--方法一：
select a.* from Test_orderByOrGroupBy_tb a where 2 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and val < a.val ) order by a.name,a.val
--方法二：
select a.* from Test_orderByOrGroupBy_tb a where val in (select top 2 val from Test_orderByOrGroupBy_tb where name=a.name order by val) order by a.name,a.val
--方法三
SELECT a.* from Test_orderByOrGroupBy_tb a where exists (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and val < a.val having Count(*) < 2) order by a.name

Sql语句代码如下：

--方法一：
select a.* from Test_orderByOrGroupBy_tb a where 2 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val ) order by a.Name,a.Val
--方法二：
select a.* from Test_orderByOrGroupBy_tb a where val in (select top 2 val from Test_orderByOrGroupBy_tb where Name=a.Name order by Val desc) order by a.Name,a.Val
--方法三：
SELECT a.* from Test_orderByOrGroupBy_tb a where exists (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val having Count(*) < 2) order by a.Name
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)

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
select a.* from Test_orderByOrGroupBy_tb a where a.Val = (select top 1 val from Test_orderByOrGroupBy_tb where a.Name = a.Name) order by a.Name

2、遵照Name分组取Val最小的值所在行的数额。

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

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

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

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

1、依据Name分组取Val最大的值所在行的数量。

4、依据Name分组随机取一条数据

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
--方法1：
select a.* from Test_orderByOrGroupBy_tb a where Val = (select min(Val) from Test_orderByOrGroupBy_tb where Name = a.Name) order by a.Name
--方法2：
select a.* from Test_orderByOrGroupBy_tb a,(select Name,min(Val) Val from Test_orderByOrGroupBy_tb group by Name) b where a.Name = b.Name and a.Val = b.Val order by a.Name
--方法3：
select a.* from Test_orderByOrGroupBy_tb a inner join (select Name,min(Val) Val from Test_orderByOrGroupBy_tb group by Name) b on a.Name = b.Name and a.Val = b.Val order by a.Name
--方法4:
select a.* from Test_orderByOrGroupBy_tb a where 1 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val < a.Val ) order by a.Name --其中1表示获取分组中前一条数据
--方法5：
select a.* from Test_orderByOrGroupBy_tb a where not exists(select 1 from Test_orderByOrGroupBy_tb where Name = a.Name and Val < a.Val)
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法1：
select a.* from Test_orderByOrGroupBy_tb a where Val = (select max(Val) from Test_orderByOrGroupBy_tb where Name = a.Name) order by a.Name
--方法2：
select a.* from Test_orderByOrGroupBy_tb a,(select Name,max(Val) Val from Test_orderByOrGroupBy_tb group by Name) b where a.Name = b.Name and a.Val = b.Val order by a.Name
--方法3：
select a.* from Test_orderByOrGroupBy_tb a inner join (select Name,max(Val) Val from Test_orderByOrGroupBy_tb group by Name) b on a.Name = b.Name and a.Val = b.Val order by a.Name
--方法4:
select a.* from Test_orderByOrGroupBy_tb a where 1 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val ) order by a.Name --其中1表示获取分组中前一条数据
--方法5：
select a.* from Test_orderByOrGroupBy_tb a where not exists(select 1 from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.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

sql如下：

：http://www.manongjc.com/article/1082.html（以转的链接为主）

select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name

select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name

select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name

select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name

create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2')
insert into tb values('a',    1,   'a1')
insert into tb values('a',    3,   'a3')
insert into tb values('b',    1,   'b1')
insert into tb values('b',    3,   'b3')
insert into tb values('b',    2,   'b2')
insert into tb values('b',    4,   'b4')
insert into tb values('b',    5,   'b5')

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
TAG标签： MySQL