国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

MSSQL 多字段根據范圍求最大值實現方法

2024-08-31 00:59:10
字體:
來源:轉載
供稿:網友

-->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
*/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平遥县| 雅江县| 安徽省| 汉沽区| 古丈县| 石门县| 响水县| 金阳县| 喀喇沁旗| 安新县| 洮南市| 信宜市| 郯城县| 和静县| 维西| 通辽市| 和平区| 宁波市| 湘潭市| 禄丰县| 涞水县| 阿克苏市| 深圳市| 南木林县| 桦南县| 延津县| 灵石县| 鲁甸县| 鹿泉市| 庐江县| 宁武县| 靖江市| 马鞍山市| 新河县| 南江县| 竹山县| 延川县| 临澧县| 达拉特旗| 砀山县| 信丰县|