如何将表的拖曳字段相加然后插入下一行? [英] how to sum tow field of table then insert into next row ?
本文介绍了如何将表的拖曳字段相加然后插入下一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨...
i想要总和字段(Sum)和字段(Add value)...然后保存在
下一行的总和:
http:// s5。 picofile.com/file/8166315884/Untitled.png
推荐答案
虽然我不能说这个解决方案是最优的,但应该完成这项工作。
试一试。
Though I can't say this solution to be optimal but should do the job.
Give a try.
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:
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 :)
谢谢Suvendu!
不要使用...
i使用这个鳕鱼:< br $>
thanks Suvendu !
butt not use ...
i use this cod :
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)
这篇关于如何将表的拖曳字段相加然后插入下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文