F1 f2 f3
---------------------
001 01 100
002 01 200
001 02 300
002 02 400
003 01 500
004 02 600
If you want to display it in the following format, should you write an SQL statement?
F1 f2 f3
---------------------
001 01 100
001 02 300
001 total 400
002 01 200
002 02 400
002 total 600
003 01 500
003 total 500
004 02 600
004 total 600
========================================================== ========================================================
Create table test
(
[ID] bigint Identity (1, 1) primary key,
F1 varchar (50 ),
F2 varchar (50 ),
F3 int
)
Drop table test
Select * from test
Insert into test values ('001', '01', 100)
Insert into test values ('002 ', '01', 200)
Insert into test values ('001', '02', 300)
Insert into test values ('002 ', '02', 400)
Insert into test values ('003 ', '01', 500)
Insert into test values ('004 ', '02', 600)
Select f1, 'Total' as f2, sum (f3) as f3 from test group by f1
Union
Select f1, f2, f3 from test