Search This Blog

Monday, February 11, 2013

SUM query in sql server


select * from table1
 
sno
letter
fileno
usertype
name
address
1
120
23
1
cp
lok
2
34
34
1
rp
lok
3
456
34
1
jon
lok
4
23
5
2
chandra
kanpur
5
67
89
1
geet
lok

 *************************************************************

select

sum(letter) as Totalletter,

sum(fileno) as Totalfile

into #temp

from table1 select #temp.*,table1.name,table1.address from #temp , table1 where table1.Usertype=2

drop table #temp

******************************************************************
select sum(letter) as totalletter,sum(fileno) as Totalfile,(select name from table1 where usertype=2)as Name,(select address from table1 where usertype=2) as Address from table1
*********************************************************************

CREATE TABLE #temp(

CID bigint Identity(1,1) Primary Key,

totalletter  int,

Totalfile  int,

Name  varchar(150),

address varchar(150))


declare @totalletter int

declare @Totalfile   int

declare @name varchar(150)

declare @Add  varchar(150)


set @totalletter =(select sum(letter) from table1)

set @Totalfile  =(select sum(fileno ) from table1)

set @name =(select top 1 name  from table1 where usertype=2)

set @Add =(select top 1 address from table1 where usertype=2)

insert  into  #temp(totalletter ,Totalfile ,Name  ,address)

values( @totalletter ,@Totalfile ,@name ,@Add )

select * from #temp

drop table #temp

**********************************OUTPUT*************************************

totalletter
Totalfile
Name
Address
700
185
chandra
kanpur

No comments :