简体   繁体   中英

PostgreSQL : How do I take multiple rows with values in different columns and combine into one row with all the values on one line?

This is the table I have currently. I would like to combine everything as one event_code and have everything in one line. I checked stack and google and got different answers which doesn't seem to solve my issue.

我现在有什么

What I would like to have is

**event_code : fs_rev : hs_rev :  pp_rev :   mp_rev : gp_rev :  in_rev : broker**

LAK180918  :  619500  :   0    :  7000   :    0     :   5000  :   8500  :   17500

I can't seem to find the answer on how to do this, by combining all the columns under one row. Can anyone suggest what i should do?

You can use Max or Min function. I am assuming you will have only one valid value for each column in the event.

select event_code, max(fs_rev),max(hs_rev),max(pp_rev),max(mp_rev)
,max(gp_rev),max(in_rev),max(broker)
from table
group by event_code

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