2017-04-27 169 views
-1

我得到我的所有列細節象下面這樣:最高值和最低值

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] 

下面是幾個例子值時,我「M查詢表1

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 

我該如何達到這個目標?

+0

我想你會需要一些動態SQL這裏,因爲它找到了最小值/最大值查詢本身值必須在飛行中生成。 –

+0

你想解決什麼問題?因爲像SQL這樣的任務在SQL之外的代碼層可能會更好地處理,因爲它不是一個真正的關係操作? –

+0

我有超過20個Millon記錄的表格,而不是逐個搜索和查找,我想要一些查詢來給出結果。 – AskMe

回答

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 @[email protected]+ 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 
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 

結果爲最大:

table1 col13 115.00 6 

結果爲分:

table1 col1 1 1 
+0

謝謝。但是,我有列表的列數在10 - 50之間變化。這是一些表格有10列,有些列有50列。可以變動嗎? – AskMe

+0

這都是你的,如果你有很多列......你可以編輯上面的答案並添加你想要的其他列。重要的是你已經有一個指導如何做到這一點。 –

-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) 

要找到最小值只是改變所有最大與最小值

相關問題