2012-07-31 43 views
0

是否可以從SQL中的數據表中獲取所有行的行總和。我有一個數據表這樣獲取表格的行總和

Date    col1   col2  col3 
30/7/2012   5    2   3 
31/7/2012   2    3   5 
01/08/2012   2    4   1 

,但我想通過創建一個列名的總 想實現這樣的:

Date   Total  col1   col2  col3 
30/7/2012   10   5    2   3 
31/7/2012   9   1    3   5 
01/08/2012  7   2    4   1 

是否有可能?如果是的話,請幫助我一樣。

+0

這是柱狀和而不是行數? – verisimilitude 2012-07-31 06:35:04

+0

@verisimilitude:我將需要每一行明智的價值.. – MAC 2012-07-31 06:37:25

回答

3

試試這個:

select Date, 
     col1 + col2 + col3 as Total, 
     col1, col2, col3 
    from your_table; 
1

你可以使用你的總DataColumnDataColumn.Expression property計算值列其他列的值的每一行的總和:

var totalColumn = new DataColumn("Total"); 
// Add your Total column to your DataTable. 
dt.Columns.Add(totalColumn); 
// The new column will be the second in the DataTable, like your diagram shows. 
totalColumn.SetOrdinal(1); 
// Use the sum of each row's Col1, Col2 and Col3 for the values in this column. 
totalColumn.Expression = "[Col1] + [Col2] + [Col3]"; 
+0

這是什麼編程語言?你爲什麼認爲OP使用它? – 2012-07-31 07:02:05

+0

@Tichodroma好點。我在這裏假設C#和.NET,因爲他使用術語'datatable',而不是'table'。如果沒有幫助,我會刪除我的答案。 – 2012-07-31 07:05:25