MSSQL 多字段根據(jù)范圍求最大值實現(xiàn)方法
-->Title:生成測試數(shù)據(jù)
-->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 --指定查詢範(fàn)圍
from @t
/*
(3 個資料列受到影響)
col1 maxcol
----------- -----------
1 60
2 60
3 56
*/
相關(guān)文章
sql server deadlock跟蹤的4種實現(xiàn)方法
一提到跟蹤倆字,很多人想到警匪片中的場景,但這里介紹的可不是一樣的哦,下面這篇文章主要給大家介紹了關(guān)于sql server deadlock跟蹤的4種實現(xiàn)方法,文中通過圖文以及示例代碼介紹的非常詳細,需要的朋友可以參考下2018-09-09SQL Server中數(shù)學(xué)函數(shù)的用法
這篇文章介紹了SQL Server中數(shù)學(xué)函數(shù)的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05SQL Server數(shù)據(jù)庫的高性能優(yōu)化經(jīng)驗總結(jié)
小編以前在做ASP及.NET的時候經(jīng)常用到SQL SERVER,現(xiàn)在用PHP雖然大多數(shù)時候用MYSQL,但不泛有些客戶要在原來SQL的平臺上升級或兼容開發(fā),值得慶幸的是PHP無所不能,基本上所有的數(shù)據(jù)庫它都能連接并支持2011-07-07Sqlserver timestamp數(shù)據(jù)類使用介紹
SQL Server timestamp 數(shù)據(jù)類型與時間和日期無關(guān)。SQL Server timestamp 是二進制數(shù)字,它表明數(shù)據(jù)庫中數(shù)據(jù)修改發(fā)生的相對順序。2011-08-08SQL設(shè)置SQL Server最大連接數(shù)及查詢語句
今天遇到了關(guān)于Sql Server最大連接數(shù)(Max Pool Size)的問題,后來通過查找一些資料解決了,所以想著總結(jié)下關(guān)于SQL Server最大連接數(shù)的內(nèi)容,所以這篇文章主要介紹了SQL設(shè)置SQL Server最大連接數(shù)與查詢語句,有需要的朋友們可以參考借鑒。2016-12-12SQL Server中的集合運算: UNION, EXCEPT和INTERSECT示例代碼詳解
這篇文章主要介紹了SQL Server中的集合運算: UNION, EXCEPT和INTERSECT,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08SQL Server 提取數(shù)字、提取英文、提取中文的sql語句
這篇文章主要介紹了SQL Server 提取數(shù)字、提取英文、提取中文 ,需要的朋友可以參考下2014-10-10