简体   繁体   中英

SQL Aggregating Mixed Data

I have a table that has 3 columns

  1. STA (which is equivalent to X)
  2. BL (which is equivalent to y)
  3. Ultimate_Load (which has positive and negative values)

I want to aggregate column 3 by the maximum of absolute values, but display the actual (negative or positive) value.

your help is appreciated

I think this would work. You get de max absolute value of ultimate_load and then change it's sign depending on the sign of MAX(ultimate_load)+MIN(ultimate_load).

SELECT STA,BL, MAX(ABS(ULTIMATE_LOAD)) * case sign(MAX(ULTIMATE_LOAD)+min(ULTIMATE_LOAD)) when 0 then 1 when -1 then -1 else 1 end as maxvalue
     FROM TAB
    GROUP BY STA, BL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM