[转]MSSQL多列取最大或者最小值
本文转自:http://blog.csdn.net/wufeng4552/article/details/4681510
/* lvl1 lvl2 lvl3 lvl4 lvl
4 3 4 1
3 2 2 1
2 2 3 4
4 4 3 4
3 1 2 2
怎么写代码 去比较lvl1、lvl2、lvl3、lvl4 对应每行的值,取其中最小的,将其值添加到lvl列里
运行结果应该是
lvl
1
1
2
3
1 */ --方法(一) 函數法 -->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-10-16 09:58:16 if not object_id('Tempdb..#t') is null
drop table #t
Go
Create table #t([lvl1] int,[lvl2] int,[lvl3] int,[lvl4] int,[lvl] int)
Insert #t
select 4,3,4,1,null union all
select 3,2,2,1,null union all
select 2,2,3,4,null union all
select 4,4,3,4,null union all
select 3,1,2,2,null
Go
if object_id('UF_minget')is not null drop function UF_minget
go
create function UF_minget
(@col1 int,@col2 int,@col3 int,@col4 int)
returns int
as
begin
declare @t table(col int)
insert @t select @col1 union all
select @col2 union all
select @col3 union all
select @col4
return(select min(col)from @t)
end
go
update t set [lvl]=dbo.UF_minget([lvl1],[lvl2],[lvl3],[lvl4])
from #t t
select * from #t
/*
lvl1 lvl2 lvl3 lvl4 lvl
----------- ----------- ----------- ----------- -----------
4 3 4 1 1
3 2 2 1 1
2 2 3 4 2
4 4 3 4 3
3 1 2 2 1 (5 個資料列受到影響) */ --方法二 MSSQL2005 XML PATH -------------------------------------
-- Author : liangCK 梁爱兰
-- Comment: 小梁 爱 兰儿
-- Date : 2009-10-16 09:57:38
------------------------------------- --> 生成测试数据: @T
DECLARE @T TABLE (lvl1 int,lvl2 int,lvl3 int,lvl4 int,lvl int)
INSERT INTO @T
SELECT 4,3,4,1,null UNION ALL
SELECT 3,2,2,1,null UNION ALL
SELECT 2,2,3,4,null UNION ALL
SELECT 4,4,3,4,null UNION ALL
SELECT 3,1,2,2,null --SQL查询如下: UPDATE A SET
lvl = B.x.value('min(//row/*)','int')
FROM @T AS A
CROSS APPLY (SELECT x = (SELECT A.* FOR XML PATH('row'),TYPE)) AS B; SELECT * FROM @T; /*
lvl1 lvl2 lvl3 lvl4 lvl
----------- ----------- ----------- ----------- -----------
4 3 4 1 1
3 2 2 1 1
2 2 3 4 2
4 4 3 4 3
3 1 2 2 1 (5 行受影响) */
--方法(三) 作者 (四方城) if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([lvl1] int,[lvl2] int,[lvl3] int,[lvl4] int,[lvl] int)
insert [tb]
select 4,3,4,1,null union all
select 3,2,2,1,null union all
select 2,2,3,4,null union all
select 4,4,3,4,null union all
select 3,1,2,2,null
go create function getmin(@a varchar(8000))
returns int
as
begin declare @ table (id int identity,a char(1))
declare @t int
insert @ select top 8000 null from sysobjects a,sysobjects b
select @t=min(cast(substring(','+@a,id+1,charindex(',',','+@a+',',id+1)-id-1) as int))
from @ where substring(','+@a,id,8000) like ',_%'
return @t
end
go -->查询
select
lvl1,
lvl2,
lvl3,
lvl4,
lvl=dbo.getmin(ltrim(lvl1)+','+ltrim(lvl2)+','+ltrim(lvl3)+','+ltrim(lvl4))
from tb /**
lvl1 lvl2 lvl3 lvl4 lvl
----------- ----------- ----------- ----------- -----------
4 3 4 1 1
3 2 2 1 1
2 2 3 4 2
4 4 3 4 3
3 1 2 2 1 (5 行受影响)
**/ --方法(四) -->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-10-16 09:58:16 if not object_id('Tempdb..#t') is null
drop table #t
Go
Create table #t([lvl1] int,[lvl2] int,[lvl3] int,[lvl4] int,[lvl] int)
Insert #t
select 4,3,4,1,null union all
select 3,2,2,1,null union all
select 2,2,3,4,null union all
select 4,4,3,4,null union all
select 3,1,2,2,null
Go if object_id('UF_minget')is not null drop function UF_minget
go
create function UF_minget
(@s varchar(200))
returns int
as
begin
return(
select col=min(substring(@s,number,charindex(',',@s+',',number)-number))
from master..spt_values
where type='p' and number<=len(@s+'a') and charindex(',',','+@s,number)=number)
end
go
select
[lvl1],
[lvl2],
[lvl3],
[lvl4],
[lvl]=dbo.UF_minget(ltrim([lvl1])+','+ltrim([lvl2])+','+ltrim([lvl3])+','+ltrim([lvl4]))
from #T
/*
lvl1 lvl2 lvl3 lvl4 lvl
----------- ----------- ----------- ----------- -----------
4 3 4 1 1
3 2 2 1 1
2 2 3 4 2
4 4 3 4 3
3 1 2 2 1 */ --方法(五) -->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-10-16 09:58:16
if not object_id('Tempdb..#t') is null
drop table #t
Go
Create table #t([lvl1] int,[lvl2] int,[lvl3] int,[lvl4] int,[lvl] int)
Insert #t
select 4,3,4,1,null union all
select 3,2,2,1,null union all
select 2,2,3,4,null union all
select 4,4,3,4,null union all
select 3,1,2,2,null
Go
select [lvl1],
[lvl2],
[lvl3],
[lvl4],
[lvl]=(select min([lvl1])
from (select [lvl1]
union all select [lvl2]
union all select [lvl3]
union all select [lvl4])T)
from #t
/*
lvl1 lvl2 lvl3 lvl4 lvl
----------- ----------- ----------- ----------- -----------
4 3 4 1 1
3 2 2 1 1
2 2 3 4 2
4 4 3 4 3
3 1 2 2 1 (5 個資料列受到影響)
*/
--方法六 作者:josy (樹哥) -->测试数据
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([lvl1] int,[lvl2] int,[lvl3] int,[lvl4] int,[lvl] int)
insert [tb]
select 4,3,4,1,null union all
select 3,2,2,1,null union all
select 2,2,3,4,null union all
select 4,4,3,4,null union all
select 3,1,2,2,null
go -->函数:返回两个数中值较小的数
if object_id('F_GetMin')is not null drop function F_GetMin
go
CREATE FUNCTION F_GetMin
(
@arg1 AS int,
@arg2 AS int
)
RETURNS int
AS
BEGIN
RETURN CASE
WHEN @arg1<=@arg2 THEN @arg1
WHEN @arg1>@arg2 THEN @arg2
ELSE NULL
END
END
GO -->查询
SELECT
lvl1,
lvl2,
lvl3,
lvl4,
lvl=dbo.f_getmin(dbo.f_getmin(dbo.f_getmin(lvl1,lvl2),lvl3),lvl4) --函数嵌套
FROM
tb -->结果
/**
lvl1 lvl2 lvl3 lvl4 lvl
----------- ----------- ----------- ----------- -----------
4 3 4 1 1
3 2 2 1 1
2 2 3 4 2
4 4 3 4 3
3 1 2 2 1 (5 行受影响)
**/
[转]MSSQL多列取最大或者最小值的更多相关文章
- MSSQL 2005 列转行应用案例
/*MSSQL 2005 列转行应用案例 By claro(陈亮) 2008-12-2 转载请包含此信息*/ --test table KuCunMX If object_id ('KuCunMX') ...
- SQL表两列取一列唯一值的记录
问下SQL表两列取一列唯一值的 A列 B列 C列 1001 AA 2012-01-02 1001 BB 2012-02-05 100 ...
- MySql多表关联,根据某列取前N条记录问题
近来遇到一个问题:“MySql多表关联,根据某列取前N条记录”. 刚开始一直在想,SQL语句是否可以做到直接查询出来,但几经折磨,还是没能写出SQL语句,-------如果有大牛的话,望指点迷津.我把 ...
- 【Oracle】oracle取最大值和最小值的几个方法汇总
(1)oracle使用keep分析函数取最值记录 -- 取工资sal最大的雇员姓名及其工资,以及工资sal最少的雇员姓名及其工资 select deptno, empno, ename, sal, m ...
- javascript 从对象数组中 按字段/属性取最大值或最小值
var array=[ { "index_id": 119, "area_id": "18335623", "name" ...
- MSSQL行车列规则
行转列,是SQL中经常会遇到的一个问题,并且分为静态转换和动态转换,所谓静态转换即在转换的行数已知或固定:动态转换则为转换的行数不固定. 转换的方法一般采用case when语句或pivot(MSSQ ...
- MSSQL 分组后取每组第一条(group by order by)
查询中经常遇到这种查询,分组后取每组第一条.分享下一个SQL语句: --根据 x 分组后.根据 y 排序后取第一条 select * from ( select ROW_NUMBER() over(p ...
- sql 对某列取值进行if判断
select if(area_id =350000, 1, 2) as area_id from my_table 取地区编号为350000的设置成 1, 其他的设置成2
- MSSQL横列转纵列
上篇我们说到了纵列转横列,这篇讲下横列转纵列,具体代码: 1.建表 CREATE TABLE [dbo].[EndLongChangeAcross]( ,) NOT NULL, ) NOT NULL, ...
随机推荐
- 应用程序有bug崩溃重启的案例2
------解决思路----------------------另外做一个服务或者程序定时监控系统进程.程序奔溃的话,都会在入口函数出现异常处理一下winform可以有两个事件来捕获主线程异常和线程异 ...
- XAlign—自动对齐代码插件
XAlign An amazing Xcode plugin to align regular code. It can align anything by using custom alignmen ...
- hdu 1087(LIS变形)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Python安装scikit-learn包
我先是按照网上说的下载了个setuptools,然后直接用这个工具去安装,可是安装scikit-learn包的时候确老是有错误,也不知道错误是啥,所以就不用setuptools来安装了. 我直接下载了 ...
- 【原创】IIS7.5优化,支持同时10万个请求
背景 IIS7.5是微软推出的最新平台IIS,性能也较以前有很大的提升,但是默认的设置配不适合很大的请求.但是我们可以根据实际的需要进行IIS调整,使其性能更佳,支持同时10万个请求. 以下方案,通过 ...
- ejs模板在express里的默认文件夹路径修改
默认的是这句: app.set('view engine','ejs') ===>/views文件夹 我想要变成/websong app.set('views','webosg'); app.s ...
- Pycharm中的Django项目连接mysql数据库
一.安装Pycharm和Django就不详细说了,自行百度 二.新建Django项目也不说了 三.配置Django连接到mysql 1.models.py写一个类,继承models.Model cla ...
- STL模板整理 全排列
概念: 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a, ...
- 转型(java)(.net)
/** * 父类 */ class Animal { public void eat() { //输出 父类吃.... } } class Bird extends Animal { public v ...
- BZOJ2120/洛谷P1903 [国家集训队] 数颜色 [带修改莫队]
BZOJ传送门:洛谷传送门 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R ...