Skip to content
Advertisement

Median of three columns (for each row) in Vertica SQL

My table looks like this:

Product Length Width Height
A 34 22 10
B 40 2 12

I would like to get the longest, second longest and third longest side for each product. For the longest and third longest it was easy, I did it like with GREATEST() and LEAST(). For the second longest for the moment I am doing it like this with a subquery, but this makes the code longer and not really clean.

The MEDIAN() function would solve my problems, but this does not accept more than one column as values. The example below unfortunately does not work. Do you know any similar function that would allow me to do that without a subquery?

Advertisement

Answer

Verticalise, use OLAP functions, then GROUP BY again:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement