1

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)

luchaninov
  • 6,792
  • 6
  • 60
  • 75
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2843969/insert-new-row-with-data-computed-from-other-rows. Also check out http://stackoverflow.com/questions/6006636/mysql-how-to-create-column-that-automatically-sums-rows – Mark Manning Jan 29 '16 at 17:15

1 Answers1

2

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

Ullas
  • 11,450
  • 4
  • 33
  • 50