简体   繁体   中英

Postgresql update in a single row

 id          name        date         time        sleep    steps     water 

"TH09062158" "RAVI" "2022-07-01" "03:54 PM" "12.00"
"TH09062158" "RAVI" "2022-07-01" "03:54 PM" "10000"
"TH09062158" "RAVI" "2022-07-01" "03:54 PM" "10"

i have created temp table and inserted from different tables but I need these data in a single row because date and time are same. Any ideas for that

If you have multiple entries with the same timestamp, user, you can group them and sum the required fields.

something like

SELECT id, name, date, time, sum(sleep), sum(steps), sum(water) GROUP BY id, name, date, time;

All columns that are not aggregated (here summed) need to be included in the GROUP BY clause.

If you want to update the values instead, you can do this instead of inserting additional entries.

UPDATE temp SET steps = steps + 500 WHERE id = 1 AND name = 'RAVI' AND date = '…' AND time = '…';

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