我如何取每个不同p_name的最大line_id值呢?
如下图:
line_id, p_name, p_price
11 aa 25
12 bb 22
13 bb 29
14 aa 30
只想用selcet语句过滤出下面结果
14 aa 30
13 bb 29
这个语句该怎么写?
14 个解决方案
#1
SELECT *
FROM tb AS A
WHERE p_price=(SELECT MAX(p_price) FROM tb WHERE A.p_name=p_name);
#2
select * from tb t where not exists(select 1 from tb where p_name=t.pname and line_id>t.line_id)
#3
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([line_id] int,[p_name] varchar(10),[p_price] int)
insert [tb] select 11,'aa',25
union all select 12,'bb',22
union all select 13,'bb',29
union all select 14,'aa',30
select *
from tb t
where not exists(
select 1
from tb
where p_name=t.p_name
and line_id>t.line_id)
order by p_name
/*
line_id p_name p_price
----------- ---------- -----------
14 aa 30
13 bb 29
(2 行受影响)
*/
#4
select line_id, p_name,max(p_price) from tb group by line_id, p_name
#5
select *
from tb t
where line_id=(
select top 1 line_id
from tb
where p_name=t.p_name
order by line_id desc)
order by p_name
/*
line_id p_name p_price
----------- ---------- -----------
14 aa 30
13 bb 29
(2 行受影响)
*/
再一种方法
#6
create table #tb (line_id int,p_name nvarchar(10),p_price int)
insert into #tb
select
11, 'aa', 25 union all select
12, 'bb', 22 union all select
13, 'bb', 29 union all select
14, 'aa', 30
select * from #tb t where t.p_price=(select max(p_price) from #tb where #tb.p_name=t.p_name)
drop table #tb
/*
结果
13 bb 29
14 aa 30
*/
#7
select * from
(select *,rn=row_number() over(partition by p_name order by p_price desc) from a) t
where rn=1
#8
select * from tb t where not exists(select 1 from tb where p_name=t.pname and line_id>t.line_id)
#9
create table chanpin(p_name varchar(30),p_price smallmoney,line_id int identity )
insert chanpin(p_name,p_price)values('aa',25)
insert chanpin(p_name,p_price)values('bb',22)
insert chanpin(p_name,p_price)values('bb',29)
insert chanpin(p_name,p_price)values('aa',30)
select p_name,max(line_id) from chanpin
group by p_name
#10
select * from
(select *,rn=row_number() over(partition by p_name order by p_price desc) a from a) t
where rn=1
(select *,rn=row_number() over(partition by p_name order by p_price desc) a from a) t
where rn=1
#11
顶一下!
#12
select * from tb a where a.p_price=(select max(p_price) from tb where a.p_name=p_name)
#13
干是一种美德
#14
刚好看看是怎么回事
#1
SELECT *
FROM tb AS A
WHERE p_price=(SELECT MAX(p_price) FROM tb WHERE A.p_name=p_name);
#2
select * from tb t where not exists(select 1 from tb where p_name=t.pname and line_id>t.line_id)
#3
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([line_id] int,[p_name] varchar(10),[p_price] int)
insert [tb] select 11,'aa',25
union all select 12,'bb',22
union all select 13,'bb',29
union all select 14,'aa',30
select *
from tb t
where not exists(
select 1
from tb
where p_name=t.p_name
and line_id>t.line_id)
order by p_name
/*
line_id p_name p_price
----------- ---------- -----------
14 aa 30
13 bb 29
(2 行受影响)
*/
#4
select line_id, p_name,max(p_price) from tb group by line_id, p_name
#5
select *
from tb t
where line_id=(
select top 1 line_id
from tb
where p_name=t.p_name
order by line_id desc)
order by p_name
/*
line_id p_name p_price
----------- ---------- -----------
14 aa 30
13 bb 29
(2 行受影响)
*/
再一种方法
#6
create table #tb (line_id int,p_name nvarchar(10),p_price int)
insert into #tb
select
11, 'aa', 25 union all select
12, 'bb', 22 union all select
13, 'bb', 29 union all select
14, 'aa', 30
select * from #tb t where t.p_price=(select max(p_price) from #tb where #tb.p_name=t.p_name)
drop table #tb
/*
结果
13 bb 29
14 aa 30
*/
#7
select * from
(select *,rn=row_number() over(partition by p_name order by p_price desc) from a) t
where rn=1
#8
select * from tb t where not exists(select 1 from tb where p_name=t.pname and line_id>t.line_id)
#9
create table chanpin(p_name varchar(30),p_price smallmoney,line_id int identity )
insert chanpin(p_name,p_price)values('aa',25)
insert chanpin(p_name,p_price)values('bb',22)
insert chanpin(p_name,p_price)values('bb',29)
insert chanpin(p_name,p_price)values('aa',30)
select p_name,max(line_id) from chanpin
group by p_name
#10
select * from
(select *,rn=row_number() over(partition by p_name order by p_price desc) a from a) t
where rn=1
(select *,rn=row_number() over(partition by p_name order by p_price desc) a from a) t
where rn=1
#11
顶一下!
#12
select * from tb a where a.p_price=(select max(p_price) from tb where a.p_name=p_name)
#13
干是一种美德
#14
刚好看看是怎么回事