Search code examples
sqlhadoophivehiveqlimpala

Trying to calculate diff between Minimum value to every value in a cell


I have two columns in table and I am trying to calculate diff between least performer to every cell in the column.

Column_a  column_B

abc       1
DEF       5
GHI       7
JKL       8

I am trying to get an output like below

abc    1   0
def    5   4
ghi    5   6
jkl    8   7

Column_c diff between each cell in column b to min(column b)


Solution

  • Use window functions:

    select column_a, column_b, (column_b - min(column_b) over ())
    from t;