ID 产品 价格
----------- ---------- --------
433334 产品1 170
435420 产品1 170
435431 产品1 154
435439 产品2 180
435440 产品2 180
435454 产品2 180
找出同一产品价格最高的记录,要求得到的结果为
ID 产品 价格
----------- ---------- --------
433334 产品1 170
435439 产品2 180
怎么写语句
----------- ---------- --------
433334 产品1 170
435420 产品1 170
435431 产品1 154
435439 产品2 180
435440 产品2 180
435454 产品2 180
找出同一产品价格最高的记录,要求得到的结果为
ID 产品 价格
----------- ---------- --------
433334 产品1 170
435439 产品2 180
怎么写语句
解决方案 »
- 在SQL Server 2005中实现数据同步问题,发布订阅。。。晕,不知道我做的错在哪里!
- 帮我看看这个存储过程应该怎么执行?
- 创建视图问题
- 字符串问题
- 一个中文字段的SQL问题
- 急,我要删除table1表中与table2表中重复的数据?两个表的关联条件是两个条件,如何写sql语句??
- 一个简单的问题
- 如何将数据表和存储过程的所有者修改为dbo用户?
- 求助:SQL Server 2012 Always On配置成功,如何测试?
- ADODB.Recordset 错误 '800a0e7d' 连接无法用于执行此操作。在此上下文中它可能已被关闭或无效。 /Conn.asp,行 71
- 请教个简单SQL,在线等,谢谢!!!
- 求一个数据库语句 高手指点
select t.* from tb where not exists (select 1 from tb where 产品 = t.产品 and 价格 > t.价格) order by t.产品
insert into tb values('433334' , '产品1' , 170 )
insert into tb values('435420' , '产品1' , 170 )
insert into tb values('435431' , '产品1' , 154 )
insert into tb values('435439' , '产品2' , 180 )
insert into tb values('435440' , '产品2' , 180 )
insert into tb values('435454' , '产品2' , 180 )
goselect t.* from tb t where 价格 = (select max(价格) from tb where 产品 = t.产品) order by t.产品
select t.* from tb t where not exists (select 1 from tb where 产品 = t.产品 and 价格 > t.价格) order by t.产品drop table tb/*
ID 产品 价格
---------- ---------- -----------
433334 产品1 170
435420 产品1 170
435454 产品2 180
435439 产品2 180
435440 产品2 180(所影响的行数为 5 行)ID 产品 价格
---------- ---------- -----------
433334 产品1 170
435420 产品1 170
435439 产品2 180
435440 产品2 180
435454 产品2 180(所影响的行数为 5 行)*/
go
create table [tb] (ID int,产品 nvarchar(6),价格 int)
insert into [tb]
select 433334,N'产品1',170 union all
select 435420,N'产品1',170 union all
select 435431,N'产品1',154 union all
select 435439,N'产品2',180 union all
select 435440,N'产品2',180 union all
select 435454,N'产品2',180
select * from tb t
where not exists(select 1 from tb where 产品=t.产品 and (价格>t.价格) or(价格=t.价格 and ID<t.ID) )
/*
ID 产品 价格
----------- ------ -----------
433334 产品1 170
435439 产品2 180(2 個資料列受到影響)*/
if object_id('[tb]') is not null drop table [tb]
create table [tb]([ID] int,[产品] varchar(5),[价格] int)
insert [tb]
select 433334,'产品1',170 union all
select 435420,'产品1',170 union all
select 435431,'产品1',154 union all
select 435439,'产品2',180 union all
select 435440,'产品2',180 union all
select 435454,'产品2',180select min([ID]) as [ID],[产品],max([价格]) as [价格]
from [tb]
group by [产品]
having count(1)>=2
------------------------433334 产品1 170
435439 产品2 180
insert into tb values('433334' , '产品1' , 170 )
insert into tb values('435420' , '产品1' , 170 )
insert into tb values('435431' , '产品1' , 154 )
insert into tb values('435439' , '产品2' , 180 )
insert into tb values('435440' , '产品2' , 180 )
insert into tb values('435454' , '产品2' , 180 )
goselect t.* from tb t where not exists (select 1 from tb where 产品 = t.产品 and (价格 > t.价格 or (价格 = t.价格 and id < t.id))) order by t.产品drop table tb/*
ID 产品 价格
---------- ---------- -----------
433334 产品1 170
435439 产品2 180(所影响的行数为 2 行)*/
go
create table [tb] (ID int,产品 nvarchar(6),价格 int)
insert into [tb]
select 433334,N'产品1',170 union all
select 435420,N'产品1',170 union all
select 435431,N'产品1',154 union all
select 435439,N'产品2',180 union all
select 435440,N'产品2',180 union all
select 435454,N'产品2',180select distinct(产品) 产品,价格 from
(
select * from tb t where not exists(select top 1 * from [tb] where 产品=t.产品 and 价格>t.价格)
) tt
--如果是sql server 2005,则如下:select ID , 产品 , 价格 from
(
select * , px = row_number() over(partition by 产品 order by 价格 desc , ID) from tb
) t
where px = 1
go
create table [tb] (ID int,产品 nvarchar(6),价格 int)
insert into [tb]
select 433334,N'产品1',170 union all
select 435420,N'产品1',170 union all
select 435431,N'产品1',154 union all
select 435439,N'产品2',180 union all
select 435440,N'产品2',180 union all
select 435454,N'产品2',180select ID,产品,价格 from
(
select ID,产品,价格,
(select count(*)+1 from [tb] where 产品=tt.产品 and ID<tt.ID) as rank
from [tb] tt
) t1
where rank=1ID 产品 价格
----------- ------ -----------
433334 产品1 170
435439 产品2 180
go
create table [tb] (ID int,产品 nvarchar(6),价格 int)
insert into [tb]
select 433334,N'产品1',170 union all
select 435420,N'产品1',170 union all
select 435431,N'产品1',154 union all
select 435439,N'产品2',180 union all
select 435440,N'产品2',180 union all
select 435454,N'产品2',180select ID,产品,价格 from tb t
where not exists(select 1 from tb where 产品=t.产品 and (价格>t.价格) or (价格=t.价格 and ID<t.ID))
/*
ID 产品 价格
----------- ------ -----------
433334 产品1 170
435439 产品2 180(所影响的行数为 2 行)*/