日期:2014-05-19  浏览次数:20377 次

如何让Datagrid 中的某行 实现合计
Eg:
现在
A       1       3       5    
A       2       3       1
A       1       1       1
B       1       1       1
B       2       2       2
想要成为:下表:

    A       1       3       5    
    A       2       3       1
    A       1       1       1
合计     4       7       7
    B       1       1       1
    B       2       2       2
合计     3       3       3

个人初步思路是让SQL写     但不知道怎么写!菜鸟求救高手!



------解决方案--------------------
select 'id ', fkid,money from b union select '合计 ' ,fkid,sum(money) as age from b group by fkid order by fkid


---把count改为sum

------解决方案--------------------
看看這個結果符合你的要求。

我在每個合計的名稱前加上了第一列的名稱。

--建立測試環境
Create Table TEST
(Col1 Varchar(10),
Col2 Int,
Col3 Int,
Col4 Int)
Insert TEST Select 'A ', 1, 3, 5
Union All Select 'A ', 2, 3, 1
Union All Select 'A ', 1, 1, 1
Union All Select 'B ', 1, 1, 1
Union All Select 'B ', 2, 2, 2
GO
--測試
Select * From TEST
Union
Select
Col1 + N '合计 ',
SUM(Col2) As Col2,
SUM(Col3) As Col3,
SUM(Col4) As Col4
From TEST
Group By Col1
--刪除測試環境
Drop Table TEST
--結果
/*
Col1 Col2 Col3 Col4
A 1 1 1
A 1 3 5
A 2 3 1
A合计 4 7 7
B 1 1 1
B 2 2 2
B合计 3 3 3
*/