惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 秋雨飘飘

显示日期的javascript日历 Iframe的基础应用——关于Iframe刷页问题的两种方法 关于left join的讨论 往表中允许为null的字段插入null值 - 秋雨飘飘 - 博客园 SQL Server CHARINDEX和PATINDEX详解 sql行列转换 sql语句优化的一些测试函数 存储过程的作用是自动生成编号 数据库管理常用SQL SQL Server 中多行多列连接成为单行单列 T-SQL 编程规范和优化技巧 (转)SQL Server 索引结构及其使用 - 秋雨飘飘 动态查询SQL中变量的返回值 存储过程的编写经验 - 秋雨飘飘 求薪水第三高的雇员的SQL题(图解) 表连接的比较left join/right join/inner join - 秋雨飘飘 sql 视图 sql 日期函数之对于周的处理 如何提升checkbox的用户体验 - 秋雨飘飘
sql查询最大最小值
秋雨飘飘 · 2007-07-20 · via 博客园 - 秋雨飘飘

曾经看到一个帖子,是问怎么一次(用一条查询语句)就查询出一个表中的最大值和最小值,其中一位这样回答的:(拿Northwind的Products表为例) 
select top 1 * from products order by UnitPrice 
union 
select top 1 * from products order by UnitPrice desc 
上面这个似乎正确,可是其实在使用了Union的时候只有最后一条Select命令才能使用Order by参数,因此上面这样是不行的,在查询分析器中运行会爆出错误 

下面提供查询出最大值和最小值的方法: 
declare @HighLow table 

ProductName 
varchar(50

insert @HighLow select top 1 Productname from Products order by Unitprice desc 
insert @HighLow select top 1 Productname from Products order by Unitprice 
select ProductName from @HighLow 
这种方法不是一次就查询出最大值和最小值,而是使用了一个Table变量,将查询出的最大值和最小值保存入这个表中。 


下面这个例子使用了Northwind数据库,取出每种书目中价格最贵的3本书: 
declare @Category table 

id 
int identity(1,1not 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=@counterorder by UnitPrice desc 
end 
select * from @MostExpensive