2017-04-27 11 views
-1

Ich bekomme alle meine Spalte Details wie unten:Höchster und niedrigster Wert aus der Tabelle SQL Server

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

meine Tabellenstruktur ist wie folgt:

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] 

Hier einige Beispielwerte, wenn ich ‚m Abfrage tabelle1

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 

wollte ich mein Ergebnis sein wie:

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 

Wie kann ich das erreichen?

+0

Ich glaube, Sie müssen einige dynamische SQL hier finden, da die Abfragen selbst, die die min/max finden Werte müssen im laufenden Betrieb generiert werden. –

+0

Was versuchen Sie zu lösen? Weil eine Aufgabe wie diese besser in einer Codelayer außerhalb von SQL behandelt werden könnte, da es sich nicht wirklich um eine relationale Operation handelt? –

+0

Ich habe Tabellen von mehr als 20Million records jeder und statt Suche und Suche eins nach dem anderen möchte ich eine Abfrage geben mir das Ergebnis wie in der Frage. – AskMe

Antwort

0

Sie können den folgenden Code verwenden, um das gewünschte Ergebnis zu erhalten. Wenn die Spaltennamen dynamisch sind, können Sie den folgenden Code einfach in dynamic sql konvertieren.

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

prüfen dies:

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 

Ergebnis für max:

table1 col13 115.00 6 

Ergebnis für min:

table1 col1 1 1 
+0

Danke. Aber, ich habe Tabellen, wo die Anzahl der Spalten zwischen 10 und 50 variiert. Das ist eine Tabelle hat 10 Spalten und einige hat 50 Spalten. Kann cols dynamisch sein? – AskMe

+0

Es gehört Ihnen, wenn Sie viele Spalten haben .. Sie können diese Antwort oben bearbeiten und andere Spalte hinzufügen, die Sie möchten. Wichtig ist, dass Sie bereits eine Anleitung haben, wie es geht. –

-1

Ich glaube, Sie dies mit zwei Schritten erreicht werden kann, zuerst Sie suchen dann den maximalen oder minimalen Wert jeder Spalte Univivot es (drehen Sie die Tabelle, so dass die Spalte wird Zeile und Zeile wird Spalte) um den gesamten max oder min Wert zu finden.

Diese Abfrage max Wert

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) 

Min mit Min nur finden Wert alle Max ändern

Verwandte Themen