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
 Calculation Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Grifter
Constraint Violating Yak Guru

263 Posts

Posted - 11/29/2013 :  10:30:23  Show Profile  Reply with Quote
Hi
I am trying to calculate an average within a query:


coalesce(field1, 0) + coalesce(field2], 0) + coalesce(field3, 0)/Count [AVG]


It seems to be subtracting the value of field3 from the total of 3 fields at some points instead of giving me the average.

Some of the results are here:


Count   Field1  Field2  Field3  Total   Average
=====   ======  ======  ======  =====   =======
2	NULL	6	NULL	6	6
11	NULL	28	13	41	29
10	NULL	33	NULL	33	33
3	NULL	12	NULL	12	12
48	NULL	108	1	109	108
7	NULL	25	NULL	25	25
1	NULL	1	NULL	1	1
3	NULL	13	NULL	13	13
141	1	1005	247	1253	1007
8	NULL	21	11	32	22
10	NULL	31	17	48	32
13	NULL	81	43	124	84
15	1	41	NULL	42	42
1	NULL	2	NULL	2	2
24	NULL	105	41	146	106
9	NULL	44	2	46	44
33	NULL	93	NULL	93	93


thanks for any input on how I may fix

G

Edited by - Grifter on 11/29/2013 10:32:45

khtan
In (Som, Ni, Yak)

Singapore
17598 Posts

Posted - 11/29/2013 :  10:52:31  Show Profile  Reply with Quote
(coalesce(field1, 0) + coalesce(field2], 0) + coalesce(field3, 0))/Count [AVG]



KH
Time is always against us

Go to Top of Page

Grifter
Constraint Violating Yak Guru

263 Posts

Posted - 11/29/2013 :  11:44:52  Show Profile  Reply with Quote
Get this:

quote:
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near 'c'.


Can't see what's wrong?

coalesce((cte.Field1, 0) + coalesce(cte.Field2, 0) + coalesce(cte.Field3, 0))/cte.Count [AVG]


Also using cross apply in query:

cross apply
(
    select cast(nullif(cte.Field1,0) as decimal(10, 2)) union all 
    select cast(nullif(cte.Field2,0) as decimal(10, 2)) union all
    select cast(nullif(cte.Field3,0) as decimal(10, 2))
) c(sum2)  
group by DiagnosisDesc,Count, [RAH22-23 Stay], [POST RAH22-23 Stay], [PRE RAH22-23 Stay]

Edited by - Grifter on 11/29/2013 11:50:04
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 11/29/2013 :  12:25:01  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Bracket in the wrong place
(coalesce(cte.Field1, 0) + coalesce(cte.Field2, 0) + coalesce(cte.Field3, 0))/cte.Count [AVG]

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Grifter
Constraint Violating Yak Guru

263 Posts

Posted - 12/02/2013 :  09:19:07  Show Profile  Reply with Quote
quote:
Bracket in the wrong place
(coalesce(cte.Field1, 0) + coalesce(cte.Field2, 0) + coalesce(cte.Field3, 0))/cte.Count [AVG]


Can't believe I missed that - thanks!

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.06 seconds. Powered By: Snitz Forums 2000