多列取最大值或者最小值

/*

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

怎么写代码去比较lvl1lvl2lvl3lvl4 对应每行的值,取其中最小的,将其值添加到lvl列里

运行结果应该是

lvl

1

1

2

3

1

*/

-方法()函数法

-->Title:Generatingtest 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

--方法二 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;
 

--方法()作者 (四方城)

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

--方法()(与方法三思路类似)

-->Title:Generatingtest 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

--方法()(与方法一思路类似)

-->Title:Generatingtest 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]
                  unionall select [lvl2]
                  unionall select [lvl3]
                  unionall select [lvl4])T)
from #t
-- 方法六   两两比较
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

from: http://bbs.csdn.net/topics/320098453

 

Pandas 是一种强大的 Python 库,专用于数据处理和分析,尤其擅长于处理表格型数据。当你想要统计 DataFrame 中某一列的数据属性时,比如出该列的最大值最小值以及平均值,Pandas 提供了方便快捷的方式来进行操作。 ### 1. 查最大值 在 Pandas 中查某一列的最大值非常简单。你需要调用 `max()` 函数并将目标列名作为参数传入。以下是示例代码: ```python # 假设 df 是 DataFrame 对象,col_name 是你想要查询的列名 max_value = df[col_name].max() ``` ### 2. 查最小值最小值的操作类似,只需要将 `max()` 替换为 `min()` 即可: ```python # 获 col_name 列的最小值 min_value = df[col_name].min() ``` ### 3. 计算平均值 计算某一列的平均值则需要用到 `mean()` 函数: ```python # 计算 col_name 列的平均值 average_value = df[col_name].mean() ``` ### 示例代码 假设我们有一个名为 `df` 的 DataFrame 和一列名为 `'age'` 的数据: ```python import pandas as pd data = { 'name': ['Alice', 'Bob', 'Charlie'], 'age': [28, 35, 42], 'height': [170, 180, 165] } df = pd.DataFrame(data) # 查 age 列的最大值 max_age = df['age'].max() print("年龄的最大值:", max_age) # 查 age 列的最小值 min_age = df['age'].min() print("年龄的最小值:", min_age) # 计算 age 列的平均年龄 avg_age = df['age'].mean() print("年龄的平均值:", avg_age) ``` 通过上述步骤,你可以很容易地对 Pandas 数据帧中的单列或多列进行各种统计操作,从而深入分析数据。这种方法不仅适用于单列,还可以扩展到多列分析,提供更为全面的数据洞察力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值