当前位置: 首页 > 图文教程 > 数据库 > MSSQL > MSSQL 多字段根据范围求最大值实现方法

MSSQL
编程管理sql server的帐号
自己改写的一个sql server 2000的分页存储过程
通过SQL定时分析表监控Unix系统性能
SQL Server磁带数据备份
构造SQL Server的安全门
利用索引提高SQLServer数据处理效率
从SQL备份文件中导入现存数据库中
SQL2005较之SQL2000的改进
SQL SERVER2000中订阅与发布的具体操作
SQL Server中解决死锁的新方法介绍
SQL注入漏洞入侵的过程及其防范措施
查询分析器设置断点单步调试存储过程
SQL Server为Web浏览器提供图像
教你在SQL Server中由原子建立分子查询
关于SQL Server业务规则链接技术探讨
SQL Server 管理常用的SQL和T-SQL
探讨SQL Server数据库中空值处理技巧
SQL Server数据库中使用触发器经验谈
四项技术提高SQL Server性能
SQL Server 2000桌面引擎默认配置空口令漏洞

MSSQL 多字段根据范围求最大值实现方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 48 ::
收藏到网摘: n/a

MSSQL 多字段根据范围求最大值实现语句,大家可以参考下

-->Title:生成測試數據
-->Author:wufeng4552
-->Date :2009-09-21 15:08:41
declare @T table([Col1] int,[Col2] int,[Col3] int,[Col4] int,[Col5] int,[Col6] int,[Col7] int)
Insert @T
select 1,10,20,30,40,50,60 union all
select 2,60,30,45,20,52,85 union all
select 3,87,56,65,41,14,21
--方法1
select [col1],
max([col2])maxcol
from
(select [col1],[col2] from @t
union all
select [col1],[col3] from @t
union all
select [col1],[col4] from @t
union all
select [col1],[col5] from @t
union all
select [col1],[col6] from @t
union all
select [col1],[col7] from @t
)T
where [col2] between 20 and 60 --條件限制
group by [col1]
/*
col1 maxcol
----------- -----------
1 60
2 60
3 56

(3 個資料列受到影響)

*/
--方法2
select [col1],
(select max([col2])from
(
select [col2]
union all select [col3]
union all select [col4]
union all select [col5]
union all select [col6]
union all select [col7]
)T
where [col2] between 20 and 60) as maxcol --指定查詢範圍
from @t
/*
(3 個資料列受到影響)
col1 maxcol
----------- -----------
1 60
2 60
3 56
*/