Elmer Duran
Elmer Duran

Reputation: 21

How to insert row into MySQL table with SUM of certain values

I have a table that looks like so

ACCOUNT AMOUNT
Income  59
Income  69
Income  99
Income  45
COGS    43
COGS    34
COGS    45
Expense 44
Expense 55

I want to insert a row into this table that will have the ACCOUNT value=Gross Profit and the AMOUNT Value = SUM(Income and COGS)

Upvotes: 1

Views: 1502

Answers (1)

Ullas
Ullas

Reputation: 11556

Use an INSERT ... SELECT Syntax

Query

INSERT INTO tblName(ACCOUNT, AMOUNT) 
SELECT 'Gross Profit',
SUM(AMOUNT)
FROM tblName
WHERE ACCOUNT IN ('Income', 'COGS');

SQL Fiddle

Upvotes: 2

Related Questions