SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


outer reference error with group by


outer reference error with group by

Author
Message
aheadley
aheadley
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 4
I get the following error: Each GROUP BY expression must contain at least one column that is not an outer reference
when I try to run this query:
INSERT INTO progsumm_dba.acc_task_sums (fiscal_year, plan_option, goal, objective_num,
task_code, account_code, person_cost, added_cost, fte_prcnt)
SELECT :fa_year, s.plan_option,s.goal, s.objective_num, s.task_code, l.account_code,
sum(s.person_cost),sum(s.added_cost), sum(ISNULL(s.fte_prcnt,0))
FROM progsumm_dba.fund_task_sums s, account_funds_lookup l
WHERE s.fiscal_year = :fa_year
AND s.plan_option = :fa_plan
AND s.fiscal_year = l.fiscal_year
AND s.plan_option = l.plan_option
AND s.fund_code = l.fund_code
GROUP BY :fa_year, s.plan_option, s.goal, s.objective_num, s.task_code, l.account_code

I have no idea what is meant by 'outer reference'. Can anyone clear this up for me?
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25813 Visits: 17509
aheadley (3/13/2013)
I get the following error: Each GROUP BY expression must contain at least one column that is not an outer reference
when I try to run this query:
INSERT INTO progsumm_dba.acc_task_sums (fiscal_year, plan_option, goal, objective_num,
task_code, account_code, person_cost, added_cost, fte_prcnt)
SELECT :fa_year, s.plan_option,s.goal, s.objective_num, s.task_code, l.account_code,
sum(s.person_cost),sum(s.added_cost), sum(ISNULL(s.fte_prcnt,0))
FROM progsumm_dba.fund_task_sums s, account_funds_lookup l
WHERE s.fiscal_year = :fa_year
AND s.plan_option = :fa_plan
AND s.fiscal_year = l.fiscal_year
AND s.plan_option = l.plan_option
AND s.fund_code = l.fund_code
GROUP BY :fa_year, s.plan_option, s.goal, s.objective_num, s.task_code, l.account_code

I have no idea what is meant by 'outer reference'. Can anyone clear this up for me?


I assume that :fa_year is a parameter? You can't group by something that is not a column in the base table. It looks like the column s.fiscal_year contains the same value so you could switch that out in your query and your group by.

Also, you should avoid using the old style syntax and switch to the newer join syntax.

Something like this should be close.


SELECT s.fiscal_year, s.plan_option,s.goal, s.objective_num, s.task_code, l.account_code,
sum(s.person_cost),sum(s.added_cost), sum(ISNULL(s.fte_prcnt,0))
FROM progsumm_dba.fund_task_sums s
INNER JOIN account_funds_lookup l on s.fiscal_year = l.fiscal_year AND s.plan_option = l.plan_option AND s.fund_code = l.fund_code
WHERE s.fiscal_year = fa_year
AND s.plan_option = fa_plan
GROUP BY s.fiscal_year, s.plan_option, s.goal, s.objective_num, s.task_code, l.account_code



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
aheadley
aheadley
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 4
Thank you. Changing to the column instead of the parameter worked. Is the new join syntax standard SQL? My programs need to run against multiple databases and I don't want to change syntax if it is going to affect running elsewhere.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25813 Visits: 17509
aheadley (3/13/2013)
Thank you. Changing to the column instead of the parameter worked. Is the new join syntax standard SQL? My programs need to run against multiple databases and I don't want to change syntax if it is going to affect running elsewhere.


Unless you have REALLY old systems you should be fine. This is the ANSI-92 syntax. It has been around for about 20 years. :-)

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search