首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一条sql语句,望各位兄弟帮忙,真分感谢解决思路

2012-01-28 
求一条sql语句,望各位兄弟帮忙,真分感谢有这样一个表IDNAMEPRICE001鞋子15.00002鞋子18.00003袜子19.00004

求一条sql语句,望各位兄弟帮忙,真分感谢
有这样一个表
ID           NAME         PRICE
001         鞋子         15.00
002         鞋子         18.00
003         袜子         19.00
004         鞋子         20.00
005         裤子         100.00
005         裤子         150.00

我想得到这样的数据:

ID           NAME         PRICE
003         袜子         19.00
004         鞋子         20.00
005         裤子         150.00

也就是每个种类里面只显示价格最高的




[解决办法]
select * from table a
where not exists(select 1 from table where iname=a.name and price> a.price)
[解决办法]
select a.* from tb a,
(select NAME , max(PRICE) price from tb group by name) b
where a.name = b.name and a.price = b.price
[解决办法]
create table #temp
(ID varchar(50),
NAME varchar(50),
PRICE decimal(10,2)
)
insert into #temp
select '001 ', '鞋子 ', '15.00 ' union all select '002 ', '鞋子 ', '18.00 ' union all select '003 ', '袜子 ', '19.00 ' union all select '004 ', '鞋子 ', '20.00 ' union all select '005 ', '裤子 ', '100.00 ' union all select '005 ', '裤子 ', '150.00 '
select * from #temp


select * from #temp a
where not exists(select 1 from #temp where name=a.name and price> a.price)

----------------------
003 袜子 19.00
004 鞋子 20.00
005 裤子 150.00

[解决办法]
if object_id( 'pubs..tb ') is not null
drop table tb
go

create table tb(ID varchar(10),NAME varchar(10),PRICE decimal(18,2))
insert into tb(ID,NAME,PRICE) values( '001 ', '鞋子 ', 15.00)
insert into tb(ID,NAME,PRICE) values( '002 ', '鞋子 ', 18.00)
insert into tb(ID,NAME,PRICE) values( '003 ', '袜子 ', 19.00)
insert into tb(ID,NAME,PRICE) values( '004 ', '鞋子 ', 20.00)
insert into tb(ID,NAME,PRICE) values( '005 ', '裤子 ', 100.00)
insert into tb(ID,NAME,PRICE) values( '005 ', '裤子 ', 150.00)

select a.* from tb a,
(select NAME , max(PRICE) price from tb group by name) b
where a.name = b.name and a.price = b.price
order by a.id

drop table tb
/*
ID NAME PRICE
---------- ---------- --------------------
003 袜子 19.00
004 鞋子 20.00
005 裤子 150.00

(所影响的行数为 3 行)
*/

热点排行