SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select sum and divide. Urgent
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

calvinkwoo3000
Yak Posting Veteran

Singapore
98 Posts

Posted - 01/06/2009 :  22:01:47  Show Profile  Reply with Quote
HI Everyone,

I want to select sum and then divide the by count as below.

col1 col2 col3 col4 .........................
______________________________________________
1 4 empty 5
2 empty 1 5
4 5 empty 5
5 2 2 empty
___________________________________________
(12)/4 = 3.0 (11/3)=3.67 (3)/2=2.50 (15)/3 = 5.0

The col1, col2, col3, col4 ....... is in nvachar type
empty mean nothing inside


Edited by - calvinkwoo3000 on 01/06/2009 22:03:31

jp2code
Posting Yak Master

USA
175 Posts

Posted - 01/06/2009 :  22:03:58  Show Profile  Visit jp2code's Homepage  Reply with Quote
I'm not so good at SQL as others on here, so I'll just say, "look into the 'cast' keyword." If I tried to tell you how to use it, I would probably be telling you wrong.


Avoid Sears Home Improvement
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

Singapore
98 Posts

Posted - 01/06/2009 :  22:10:31  Show Profile  Reply with Quote
i tried this but incorrect.

SELECT
ROUND(SUM(CAST(ISNULL(Col1, 0) AS float)) / COUNT(*), 2) AS [Total Salary],
ROUND(SUM(CAST(ISNULL(Col2, 0) AS float)) / COUNT(*), 2) AS [Total Salary2],
ROUND(SUM(CAST(ISNULL(Col3, 0) AS float)) / COUNT(*), 2) AS [Total Salary3],
ROUND(SUM(CAST(ISNULL(Col4, 0) AS float)) / COUNT(*), 2) AS [Total Salary4]

FROM table
WHERE
(Col1 <> '') and
(Col2 <> '') and
(Col3 <> '') and
(Col4 <> '')

anyone can help in this?

Edited by - calvinkwoo3000 on 01/06/2009 22:19:37
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

Singapore
98 Posts

Posted - 01/06/2009 :  22:21:14  Show Profile  Reply with Quote
SELECT
ROUND(SUM(CAST(ISNULL(Col1, 0) AS float)) / COUNT(*), 2) AS [Total Salary],

FROM table
WHERE
(Col1 <> '')

This is correct but only one by one colomn, i have about 80 column.
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

India
333 Posts

Posted - 01/06/2009 :  22:43:30  Show Profile  Reply with Quote
select sum(cast(coalesce(col1,0))as numeric(18,2))/count(col1) as Totalsalary from urtable

Jai Krishna
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

Singapore
98 Posts

Posted - 01/07/2009 :  01:20:31  Show Profile  Reply with Quote
quote:
Originally posted by Jai Krishna

select sum(cast(coalesce(col1,0))as numeric(18,2))/count(col1) as Totalsalary from urtable

Jai Krishna



but how about another col?
Go to Top of Page

bklr
Flowing Fount of Yak Knowledge

India
1693 Posts

Posted - 01/07/2009 :  02:17:17  Show Profile  Reply with Quote
select sum(CONVERT(numeric(18,2),ISNULL(col1,0) ))/count(col1),sum(convert(numeric(18,2),ISNULL(col2,0)))/count(col2),....... from urtable
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

India
333 Posts

Posted - 01/07/2009 :  02:24:00  Show Profile  Reply with Quote
quote:
Originally posted by calvinkwoo3000

quote:
Originally posted by Jai Krishna

select sum(cast(coalesce(col1,0))as numeric(18,2))/count(col1) as Totalsalary from urtable

Jai Krishna



but how about another col?



Include every column in ur select list

Jai Krishna
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/07/2009 :  03:03:53  Show Profile  Reply with Quote
quote:
Originally posted by calvinkwoo3000

HI Everyone,

I want to select sum and then divide the by count as below.

col1 col2 col3 col4 .........................
______________________________________________
1 4 empty 5
2 empty 1 5
4 5 empty 5
5 2 2 empty
___________________________________________
(12)/4 = 3.0 (11/3)=3.67 (3)/2=2.50 (15)/3 = 5.0

The col1, col2, col3, col4 ....... is in nvachar type
empty mean nothing inside




SELECT SUM(col1)*1.0/count(col1) as col1,
SUM(col2)*1.0/count(col2) as col2,
SUM(col3)*1.0/count(col3) as col3,
SUM(col4)*1.0/count(col4) as col4
FROM Table
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22769 Posts

Posted - 01/08/2009 :  03:15:28  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30281 Posts

Posted - 01/08/2009 :  03:22:30  Show Profile  Visit SwePeso's Homepage  Reply with Quote
What is wrong with Average?
SELECT	AVG(1.0E * NULLIF(Col1, '')) AS Col1,
	AVG(1.0E * NULLIF(Col2, '')) AS Col2,
	AVG(1.0E * NULLIF(Col3, '')) AS Col3,
	AVG(1.0E * NULLIF(Col4, '')) AS Col4
FROM	Table1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

calvinkwoo3000
Yak Posting Veteran

Singapore
98 Posts

Posted - 01/14/2009 :  00:43:04  Show Profile  Reply with Quote
Thanks all of you.
i get it alreaady
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/14/2009 :  02:19:53  Show Profile  Reply with Quote
welcome
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000