Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Variables and nested query returning 0

Author  Topic 

NEVerton
Starting Member

9 Posts

Posted - 2012-02-10 : 17:23:31
I am new to SQL and want to understand why this does not work - it returns a zero value. There are loads of values in the call_events table... please tell me what i am doing wrong. Thanks.

***Example 1***

Declare @closing_wip INT

select @closing_wip = (select sum(event_cost) from call_events where link_to_call = call_ref)
from calls

select @closing_wip


sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-10 : 17:45:55
Which tables do the columns in the where condition belong to?

If there are link_to_call and call_ref columns in the call_events table, it will be trying to compare the values from those two columns in call_events table.

If one or the other column is not in the call_events table, but is in the calls table, then it will be a correlated query - which could return multiple results. If so, @closing_wip will be assigned one among those results, and if that happens to be zero (because the event_cost happened to be zero for that call_ref), you will get a null.

Can you describe what you are trying to do? Some sample data would help as well.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-02-13 : 11:05:14
See what this returns

select sum(event_cost) from call_events where link_to_call = call_ref

Madhivanan

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

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-13 : 11:14:17
What does this give you?

SELECT link_to_call, sum(event_cost)
FROM call_events
GROUP BY link_to_call

Do you need quotes?

link_to_call = 'call_ref'

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -