SQL查詢最大最小值的示例
作者:佚名
下面以實(shí)例的形式為您介紹SQL查詢最大最小值的實(shí)現(xiàn)方法,供您參考,希望對(duì)您學(xué)習(xí)SQL查詢方面能夠有些許幫助。
下面這個(gè)SQL查詢***最小值的例子使用了Northwind數(shù)據(jù)庫(kù),取出每種書目中價(jià)格最貴的3本書,希望可以讓您對(duì)SQL查詢有更多的認(rèn)識(shí)。
- declare @Category table
- (
- id int identity(1,1) not null,
- CategoryId int,
- CategoryName varchar(50)
- )
- declare @MostExpensive table
- (
- ProductName varchar(50)
- )
- declare @counter int,@number int
- set @counter=0
- insert @Category select CategoryId,CategoryName from Categories
- select @number=count(*) from @Category
- while @counter<@number
- begin
- set @counter=@counter+1
- insert @MostExpensive select top 3 ProductName from products where categoryid=(select CategoryId from @Category where id=@counter) order by UnitPrice desc
- end
- select * from @MostExpensive
【編輯推薦】
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)