SqlServer查询多列的列名跟最大最小值
SqlServer查询多列的列名和最大最小值
比如:
Table A:
Id Col1 Col2
1 121 11
2 5 23
3 21 111
我想得到:
Name Max Min
Col1 121 5
Col2 111 11
------解决思路----------------------
select 'Col1' as Name,max(col1) as Max,min(col1) as Min
union all
select 'Col2' as Name,max(col2) as Max,min(col2) as Min
------解决思路----------------------
如果有几十个列呢??
用的sql2008?
结贴吧,
比如:
Table A:
Id Col1 Col2
1 121 11
2 5 23
3 21 111
我想得到:
Name Max Min
Col1 121 5
Col2 111 11
------解决思路----------------------
select 'Col1' as Name,max(col1) as Max,min(col1) as Min
union all
select 'Col2' as Name,max(col2) as Max,min(col2) as Min
------解决思路----------------------
如果有几十个列呢??
用的sql2008?
SELECT colName as Name, max(value) as maxValue,min(value) as minValue
FROM tableA
UNPIVOT
(
value FOR colName IN([col1], [col2])
) AS UPV
group by colName
/*
Name maxValue minValue
col1 121 5
col2 111 11
*/
结贴吧,