Click here to Skip to main content
15,901,792 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi ...
i want sum field (Sum) and field (Add value) ... then save in
Sum from next Row :


http://s5.picofile.com/file/8166315884/Untitled.png
Posted
Comments
Kornfeld Eliyahu Peter 26-Jan-15 4:06am    
You should search for 'running total'...
Suvendu Shekhar Giri 26-Jan-15 4:32am    
What DBMS you are using? SQL Server? MySQL?

Though I can't say this solution to be optimal but should do the job.
Give a try.

SQL
SELECT T1.ID,
        ISNULL((SELECT SUM(T2.AddValue)
         FROM   [TableName] T2
         WHERE  T2.ID < T1.ID),0) AS [Sum]
         ,T1.AddValue
FROM   [TableName] AS T1
UNION
SELECT MAX(ID)+1,SUM(AddValue),NULL FROM [TableName]


Here is an example for your reference:
SQL
DECLARE @TBL TABLE (ID INT, AddValue INT)
INSERT INTO @TBL
    SELECT 1 ID, 255 AddValue
    UNION
    SELECT 2, 45
    UNION
    SELECT 3, 200

SELECT T1.ID,
        ISNULL((SELECT SUM(T2.AddValue)
         FROM   @TBL T2
         WHERE  T2.ID < T1.ID),0) AS [Sum]
         ,T1.AddValue
FROM   @TBL AS T1
UNION
SELECT MAX(ID)+1,SUM(AddValue),NULL FROM @TBL


Output:
ID	   Sum	      AddValue
1	   0	      255
2	   255	      45
3	   300	      200
4	   500	      NULL


Hope it helps :)
 
Share this answer
 
thanks Suvendu !
butt not use ...

i use this cod :

C#
private void button5_Click(object sender, EventArgs e) 
        { 
            int a = 0; 
            try 
            { 
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Shopping;Integrated Security=True"); 
                SqlCommand com = new SqlCommand(); 
                com.Connection = con; 
                com.CommandText = "SELECT TOP 1 Mojodi FROM TMojojdi ORDER BY Id DESC"; 
                con.Open(); 
                SqlDataReader dr = com.ExecuteReader(); 
                if (dr.Read()) 
                { 
                    a = Int32.Parse(dr[0].ToString()); 
                } 
                con.Close(); 
            } 
            catch 
            { 

            } 
            finally 
            { 
                con.Close(); 
                con.Dispose(); 
            } 

            try 
            { 
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Shopping;Integrated Security=True"); 
                SqlCommand com = new SqlCommand(); 
                com.Connection = con; 
                com.CommandType = CommandType.Text; 
                com.CommandText = "Insert Into TMojojdi (Id,Mojodi,Ad,Kam) Values (@i,@m,@a,@k)"; 
                com.Parameters.AddWithValue("@i", textBox6.Text); 
                com.Parameters.AddWithValue("@m", a = a - int.Parse(textBox7.Text)); 
                com.Parameters.AddWithValue("@a", 0); 
                com.Parameters.AddWithValue("@k", textBox7.Text); 
                con.Open(); 
                com.ExecuteNonQuery(); 
                con.Close(); 
                con.Dispose(); 
                MessageBox.Show("Bardasht As Mojodi Ok", "Bardasht"); 
                ShowGrid2(); 
            } 
            catch 
            { 

            } 
            finally 
            { 
                con.Close(); 
                con.Dispose(); 
            } 
        } 


how change this code with use your query (union query)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900