sql server - SQL query to display all columns with min, max and avg values from table -
please query below table
b c d ----------------- 1 5 7 -5 2 6 8 7
i need minimum, maximum , average values columns listed vertically. output should below. x, x, x sample letter instead of real min, max , avg values.
cn min max avg ---------------------- x x x b x x x c x x x d x x x
cn = column name
can give me query this?
something maybe:
;with cte ( select 'a' cn, tbl.a nbr tbl union select 'b', tbl.b tbl union select 'c', tbl.c tbl union select 'd', tbl.d tbl ) select cte.cn, max(cte.nbr) max, min(cte.nbr) min, avg(cte.nbr) avg cte group cte.cn
or unpivot:
select unpvt.cn, max(unpvt.nbr) max, min(unpvt.nbr) min, avg(unpvt.nbr) avg ( select tbl.a, tbl.b, tbl.c, tbl.d tbl ) p unpivot ( nbr cn in (a,b,c,d) ) unpvt group unpvt.cn
Comments
Post a Comment