0

I am a beginner programmer wanna ask about simple SUM query for C#. here is the case:

I have a table called "revenue", and that table consist of 5 columns. they are Bulan, Target, Realisasi, Target_YtD, and Realisasi_YtD. for column Bulan, I manually inserted 12 data. they are January, Februari, March, and so on...

For column Target and Realisasi also I inserted data manually with INT data type.

Now, I wanna add up the January's Target + February's Target + March's Target, and then the value of that calculation is gonna fill March's Target_YtD.

Can somebody tell me the query of that? I hope anyone can help me this time, I really appreciate that. Thanks

user3378165
  • 6,546
  • 17
  • 62
  • 101
  • 1
    Seems like a simple database statement, why do you need C# for it? – nvoigt Aug 23 '16 at 06:18
  • 2
    Have a read through this: http://stackoverflow.com/help/how-to-ask – Ash Aug 23 '16 at 06:21
  • if you want sql query , you may look in to this, possible duplicate http://stackoverflow.com/questions/1607720/sql-server-sum-of-multiple-rows-including-where-clauses – Meena Aug 23 '16 at 06:30

2 Answers2

1

Do you mean something like this?

UPDATE 
SET Target_YtD =
  (SELECT SUM(Target) AS Total FROM revenue
  WHERE Bulan IN ('JAnuary', 'February', 'March')
)
WHERE Bulan = 'March'
0

It depends on if you plan to calculate the colum at the time of input and save it to the database. Usually calculated columns are not in and of themselves columns in databases, but if that is what you are doing you can just use the c# concatenation function to populate that variable before doing your insert statement. If you're only populating the sum upon output to a c# app, same thing applies, or you could do a sum in your sql code. A few ways to get there, just depends what you're trying to do.

MydKnight
  • 301
  • 2
  • 12