Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a table like this.

C#
Sno  |  Sname  |  Marks  
001  |  abcde  |  90
002  |  fghij  |  80
003  |  lmnop  |  70
004  |  qrstu  |  60
005  |  vwxyz  |  50



I want to make a total like this

C#
Sno  |  Sname  |  Marks     |  Total  
001  |  abcde  |  90        |  -
002  |  fghij  |  80        |  -
003  |  lmnop  |  70        |  -
004  |  qrstu  |  60        |  -
005  |  vwxyz  |  50        |  350


Please help in this...

Thanks in advance...
Posted
Updated 17-Sep-13 21:24pm
v3

Something like this:
SQL
SELECT 
sub1,
sub2,
sub3,
(sub1 + sub2 + sub3) as 'Total Marks'
FROM Student
 
Share this answer
 
Try below snippet. It uses ROLLUP feature.

SQL
DECLARE @Table Table (Sno VARCHAR(3) , Sname VARCHAR(20),  Marks  INT)
INSERT INTO @Table VALUES ('001'  ,  'abcde'  ,  90)
,('002'  ,  'fghij'  ,  80)
,('003'  ,  'lmnop'  ,  70)
,('004'  ,  'qrstu'  ,  60)
,('005'  ,  'vwxyz'  ,  50)

SELECT Sno, SUM(Marks) Marks
FROM @Table
GROUP BY Sno WITH ROLLUP


This will give you output like

Sno  Marks
---- -----------
001  90
002  80
003  70
004  60
005  50
NULL 350
 
Share this answer
 
Comments
nsvrao 18-Sep-13 5:37am    
thanks for your solution... but according to the requirement, I am not supposed to declare another table please give me a query with in the select command it self... if possible
Saral S Stalin 18-Sep-13 6:38am    
Dear, I just gave you a hint about ROLLUP feature. You do not need to declare another table, you can use this against your existing table only. But I will not be doing the coding for you.
gauravupadhyay 18-Sep-13 7:11am    
rollup will not give him desired result what he wants is
Sno | Sname | Marks | Total
001 | abcde | 90 | -
002 | fghij | 80 | -
003 | lmnop | 70 | -
004 | qrstu | 60 | -
005 | vwxyz | 50 | 350


while rollup add one new row to the table .
Hi nsvrao please use this sql script i tested it and its result as u desire .

SQL
SELECT
sno ,Sname , marks ,
'totalmarks'=
 CASE
 WHEN convert(int , sno)   = ( SELECT count(*) FROM tbl_test)
 THEN
   CONVERT(varchar , (SELECT sum(marks) FROM tbl_test))
   ELSE
   '-'
   END
  FROM tbl_test



i am working on simplification of this select query if possible

regards
 
Share this answer
 
v2
Comments
Raja Sekhar S 20-Sep-13 5:01am    
In your Query for each and every value the select count(*) will be executed... if there are 10 records in that table... select count(*) will run for 10 times... and if the sno is not Order wise then also some issues will arise...
SQL
Create Table #Temp
	(
	 sno Int,
	 Sname varchar(40),
	 Marks Int
	)

Insert into #Temp 
Values(001,'abcde',90),(002,'fghij',80),(003,'lmnop',70),(004,'qrstu',60),(005,'vwxyz',50)

;with cte
as
(
Select sno,Sname,Marks,ROW_NUMBER() Over(Order by Sno) rno From #Temp
)

Select st.sno,st.Sname,st.Marks,COALESCE(CONVERT(VARCHAR,t.Total),'--') [Total]  from cte st
left outer join (Select sum(Marks) [Total],max(rno) [rno] From cte ) t
on t.rno=st.rno

Drop Table #Temp 

Output:
SQL
sno	Sname	Marks	Total
---    ------- ----- --------
1	abcde	90	--
2	fghij	80	--
3	lmnop	70	--
4	qrstu	60	--
5	vwxyz	50	350
 
Share this answer
 
SELECT SUM(Marks) FROM table_name;
u can find out sum of a column like this method
 
Share this answer
 
Comments
nsvrao 18-Sep-13 4:28am    
Thanks for your solution...
But I want that sum in another column in the last row
the remaining columns should be "-".

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