Home > Net >  How to SUM() a column and write to data table
How to SUM() a column and write to data table

Time:09-03

So i'm a newbie at programming and i need to store data.I have created Service-Based Database for store the data and i have a table like this.

gameprice gamedesc gametotal
60        RDR2 
40        R6S

i want to take gameprice column, sum it, and write to gametotal as a 1 row. I mean even if i add another game it will add new game price to current gametotal. There is have 2 problems. I dont know sql and

Select Sum(gameprice) as gametotal From gametable

creating another column named gametotal and not saving or im writing it wrong place.I'm using Winforms.

ex. First Second

CodePudding user response:

Are you looking answer to be like below

gameprice    gamedesc
40          R6S
60          RDR2
100         Gametot

If yes you can use below query

SELECT * FROM [YourTable] UNION ALL SELECT SUM(gameprice), 'Gametot' AS gamedesc FROM [YourTable]

CodePudding user response:

-- make gt variable
DECLARE @gt INT;

-- clear GameTotal Column 
UPDATE GameTable SET GameTotal = NULL 

-- set gt value to game cloumn total value
select @gt = sum(game) from GameTable;

-- inster @gt to GameTotal Column
INSERT INTO FoodTable (GameTotal) values (@gt);

taking gameprice column total value, clearing Game Total previous value and entering new value

  • Related