表sql server中的最高和最低值

时间:2017-04-27 05:38:53

标签: sql-server sql-server-2008

我的所有专栏详情如下:

SELECT COLUMN_NAME FROM  INFORMATION_SCHEMA.COLUMNS  
  WHERE TABLE_NAME = 'table1'
    AND DATA_TYPE IN ('int','decimal','numeric')

我的表格结构如下:

CREATE TABLE [dbo].[table1](
[col1] int not NULL,
[col2] [numeric](7, 0) NULL,
[col3] [varchar](30) NULL,
[col4] [varchar](8) NULL,
[col5] [varchar](2) NULL,
[col6] [varchar](8) NULL,
[col7] [varchar](3) NULL,
[col8] [varchar](7) NULL,
[col9] [varchar](5) NULL,
[col10] [varchar](8) NULL,
[col11] [numeric](7, 0) NULL,
[col12] [numeric](7, 3) NULL,
[col13] [numeric](7, 2) NULL,
[col14] [decimal](7, 2) NULL,
[col15] [varchar](1) NULL,
) ON [PRIMARY]

以下是我查询table1时的几个示例值

 col1   col2     col11      col12       col13      col14
 ------------------------------------------------------------
 1      10.0      80.00      10.000      12.00      90.00
 2      70.0      10.00      97.960      14.00      10.00
 3      30.00     12.00      14.000      115.00     11.00
 4      40.00     11.00      15.000      15.80      12.00

我希望我的结果如下:

 for max:

Table_name   max_col_name     max_col_value     max_col_value_length
---------------------------------------------------------------------
 table1       col3               115.00               6


 for min value:

Table_name   min_col_name     min_col_value     min_col_value_length
---------------------------------------------------------------------
 table1       col1               1                   1

我怎样才能实现这个目标?

3 个答案:

答案 0 :(得分:0)

您可以使用以下代码来获得所需的结果。如果列名是动态的,那么您可以轻松地将以下代码转换为动态sql。

CREATE TABLE [dbo].[table1](
[col1] int not NULL,
[col2] [numeric](7, 0) NULL,
[col3] [varchar](30) NULL,
[col4] [varchar](8) NULL,
[col5] [varchar](2) NULL,
[col6] [varchar](8) NULL,
[col7] [varchar](3) NULL,
[col8] [varchar](7) NULL,
[col9] [varchar](5) NULL,
[col10] [varchar](8) NULL,
[col11] [numeric](7, 0) NULL,
[col12] [numeric](7, 3) NULL,
[col13] [numeric](7, 2) NULL,
[col14] [decimal](7, 2) NULL,
[col15] [varchar](1) NULL,
) ON [PRIMARY]
GO

insert into  table1 (col1,col2,col11,col12,col13,col14)  values 
(1,      10.0  ,    80.00,      10.000  ,    12.00    ,  90.00),
(2,     70.0  ,    10.00  ,    97.960    ,  14.00     , 10.00),
(3,      30.00,     12.00  ,    14.000    ,  115.00   ,  11.00),
(4,      40.00,     11.00   ,   15.000     , 15.80    ,  12.00)  

declare @sql varchar(max)
declare @col varchar(max)=''
select @col=@col+ COLUMN_NAME +', ' FROM  INFORMATION_SCHEMA.COLUMNS  
  WHERE TABLE_NAME = 'table1'
    AND DATA_TYPE IN ('int','decimal','numeric')

SELECT ColumnName,ColumnValue 
into #TableMax
from (select cast(max(col1) as numeric(7,3))C1, CAST(max(col2) as numeric(7,3))C2, 
                                           cast(max(col11) as numeric(7,3))C11, CAST(max(col12) as numeric(7,3))C12, 
                                           CAST(max(col13) as numeric(7,3))C13, CAST(Max(col14) as numeric(7,3))C14

from table1) T
 Unpivot(ColumnValue For ColumnName IN (C1,C2,C11,C12,C13,C14)) AS H

SELECT ColumnName,ColumnValue 
into #TableMin
from (select cast(Min(col1) as numeric(7,3))C1, CAST(Min(col2) as numeric(7,3))C2, 
                                           cast(Min(col11) as numeric(7,3))C11, CAST(Min(col12) as numeric(7,3))C12, 
                                           CAST(Min(col13) as numeric(7,3))C13, CAST(Min(col14) as numeric(7,3))C14

from table1) T
 Unpivot(ColumnValue For ColumnName IN (C1,C2,C11,C12,C13,C14)) AS H

select top 1 columnname max_columnname,columnvalue max_columnvalue,len(columnvalue)max_col_value_length from  #TableMax order  by columnvalue desc
select top 1 columnname min_columnname,columnvalue min_columnvalue,len(columnvalue)min_col_value_length from  #TableMin order  by columnvalue 
drop table table1
drop table #TableMax
drop table #TableMin

答案 1 :(得分:0)

检查一下:

DECLARE @TB TABLE(col1 int, col2 decimal(10,2), col11 decimal(10,3), col12 decimal(10,2),col13 decimal(10,2),col14 decimal(10,2))
insert into @tb
select  1 ,     10.0   ,   80.00   ,   10.000  ,    12.00  ,    90.00 union all
select   2 ,     70.0   ,   10.00  ,    97.960  ,    14.00  ,    10.00 union all
select   3 ,     30.00  ,   12.00  ,    14.000  ,    115.00 ,    11.00 union all
select   4  ,    40.00  ,   11.00  ,    15.000 ,     15.80  ,    12.00





select 'table1' as Table_Name,b.column_name as max_col_name, cast(a.max_col as decimal(10,2)) as max_col_value , len(cast(a.max_col as decimal(10,2))) as  max_col_value_length from
(select max(max_col1) as max_col from
(select max(col1) as max_col1,'col1' as column_name from @tb union all
select max(col2) as max_col2,'col2' from @tb union all
select max(col11)as max_col11,'col11'  from @tb union all
select max(col12)as max_col12,'col12' from @tb union all
select max(col13)as max_col13,'col13' from @tb union all
select max(col14)as max_col14,'col14' from @tb) as a) as a

left join

(select max(max_col1) as max_col,column_name from
(select max(col1) as max_col1,'col1' as column_name from @tb union all
select max(col2) as max_col2,'col2' from @tb union all
select max(col11)as max_col11,'col11'  from @tb union all
select max(col12)as max_col12,'col12' from @tb union all
select max(col13)as max_col13,'col13' from @tb union all
select max(col14)as max_col14,'col14' from @tb) as a group by column_name) as b
on a.max_col = b.max_col








select 'table1' as Table_Name,b.column_name as min_col_name, cast(a.min_col as integer) as min_col_value , len(cast(a.min_col as integer)) min_col_value_length from
(select min(min_col1) as min_col from
(select min(col1) as min_col1,'col1' as column_name from @tb union all
select min(col2) as min_col2,'col2' from @tb union all
select min(col11)as min_col11,'col11'  from @tb union all
select min(col12)as min_col12,'col12' from @tb union all
select min(col13)as min_col13,'col13' from @tb union all
select min(col14)as min_col14,'col14' from @tb) as a) as a

left join

(select min(min_col1) as min_col,column_name from
(select min(col1) as min_col1,'col1' as column_name from @tb union all
select min(col2) as min_col2,'col2' from @tb union all
select min(col11)as min_col11,'col11'  from @tb union all
select min(col12)as min_col12,'col12' from @tb union all
select min(col13)as min_col13,'col13' from @tb union all
select min(col14)as min_col14,'col14' from @tb) as a group by column_name) as b
on a.min_col = b.min_col

结果为max:

table1  col13   115.00  6

结果为min:

table1  col1    1   1

答案 2 :(得分:-1)

我认为您可以通过两个步骤实现此目的,首先搜索每列的最大值或最小值然后将其取消(旋转表以使列成为行和行成为列)以查找总体最大值或最小值。

此查询将找到最大值

 WITH cte1 AS (SELECT 'table1' AS Table_name 
                      ,max_col_name
                      ,max_col_value
                      ,LEN(max_col_value) AS max_col_value_length 
               FROM
                   (SELECT col2,col3,col4 
                     FROM 
                     (SELECT MAX(col2) AS col2 
                            ,MAX(col3) AS col3
                            ,MAX(col4) AS col4 FROM table1) x) y
                    UNPIVOT
                    (Nilai FOR max_col_name IN (col2,col3,col4)) unpvt) 

SELECT * FROM cte
WHERE max_col_value = (SELECT MAX(max_col_value) FROM cte)

要查找最小值,只需将最小值更改为最小值