SQL adding columns together

ziggy

I have a PostgreSQL table of 7k records. each record has 3 a unique ID and 3 fields with it. childcares, shcools, hospitals. there are all integer fields. i want to add a new column and calculate the total amount of receptors (schools,childcares,hospitals) for each row. I thought this should be pretty straighforward with adding a column and doing an insert with a select but i am not getting the results i want

alter table site add total integer;

insert into site(total) select sum(schools+childcares+hospitals) as s from site;

i have also tried a group by id in the insert select statement

Pரதீப்

You are looking for Update not Insert

Update site 
 set total = COALESCE(schools,0)+COALESCE(childcares,0)+COALESCE(hospitals,0)

Added COALESCE to handle NULL values.

Ex :

1 + 2 + NULL = NULL so to replace NULL with 0 I have used COALESCE.

Now it will be 1 + 2 + 0(NULL) = 3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL Adding columns together

From Dev

MySQL Adding columns together

From Dev

Adding all columns together?

From Dev

Adding two time columns together in pandas dataframe?

From Dev

Adding two time columns together in pandas dataframe?

From Dev

subracting two columns after firstly adding two columns together

From Dev

SQL add columns of each record together

From Dev

SQL Server Join on Multiple Columns Joining Together

From Dev

Adding two columns together in CSV and outputting to new CSV file

From Dev

Adding together 4 different Tablix data columns SSRS

From Dev

Adding extracted columns to a table - SQL

From Dev

Adding data in sql Table columns

From Dev

Adding extracted columns to a table - SQL

From Dev

Select columns from a view and tables together and multiply columns in sql

From Dev

Adding COUNT together when grouping twice SQL Server

From Dev

SQL query by adding two columns in where clause?

From Dev

SQL query by adding two columns in where clause?

From Dev

(SQL) Adding elements in columns if they have the same name

From Dev

SQL adding two columns and group by count

From Dev

Possible to add multiple columns together and Alias in PL/SQL?

From Dev

Adding together timedeltas in Python

From Dev

Adding strings in lists together

From Dev

Adding together values of ENUMs

From Dev

Adding label text together

From Dev

Adding Android views together

From Dev

adding parts of a javabean together

From Dev

Adding strings together in CUDA?

From Dev

Adding together calculated variables

From Dev

Datatables - Merge columns together